cemg
cemg

Reputation: 73

Excel VBA code to open a file

I created a macro button to open my daily files from a excel production sheet where I have all the my macro button for specific files.

The format for all my files are conventionally the same:

  1. Businese Unit Name: YMCA
  2. Year:2012
  3. Month: April
  4. Week: Week 2
  5. Day: 12
  6. File Name: YMC Template 041212.xlsm

I am having issue with the last excel file name extension. how do I add the MyDaily Template and MyDateProd along with the .xlsm. I have this -J:.....\& myDailyTemplate & myDateProd.xlsm") see below for entire file path names.

Sub Open_DailyProd()

    Dim myFolderYear As String
    Dim myFolderMonth As String
    Dim myFolderWeek As String
    Dim myFolderDaily As String
    Dim myDateProd As String
    Dim myBusinessUnit As String
    Dim myDailyTemplate As String

    myBusinessUnit = Sheet1.Cells(32, 2)
    myFolderYear = Sheet1.Cells(11, 2)
    myFolderMonth = Sheet1.Cells(12, 2)
    myFolderWeek = Sheet1.Cells(13, 2)
    myFolderDaily = Sheet1.Cells(14, 2)
    myDateProd = Sheet1.Cells(15, 2)
    myDailyTemplate = Sheet1.Cells(6, 5)

    Application.Workbooks.Open ("J:\IAS\3CMC05HA01\IAC Clients\myBusinessUnit\myFolderYear\myFolderMonth\myFolderWeek\myFolderDaily\& myDailyTemplate & myDateProd.xlsm")

End Sub

Upvotes: 1

Views: 57848

Answers (1)

psubsee2003
psubsee2003

Reputation: 8741

Excel is looking for a file called: "J:\IAS\3CMC05HA01\IAC Clients\myBusinessUnit\myFolderYear\myFolderMonth\myFolderWeek\myFolderDaily\& myDailyTemplate & myDateProd.xlsm"

since that is what is included in the quotes, but from your code, you appear to have a number of variables that are part of this string, you need to take them out of the quotes and concatenate them together. Try something like this:

"J:\IAS\3CMC05HA01\IAC Clients\" & myBusinessUnit & "\" & myFolderYear _
& "\" & myFolderMonth & "\" & myFolderWeek & "\" & myFolderDaily & _
"\" & myDailyTemplate & myDateProd & ".xlsm"

I added the continuation _ to make it more readable onthe screen here, but it is not necessary, you can put everything on one line together if you prefer.

Unless you need all of the myBusinessUnit, myFolderYear, etc variables elsewhere, I would think about doing it in some sort of array and then doing a Join function to concatenate everything. I, personally, find this easier to maintain going forward and easier to see the hierarchy in the folder structure rather than looking at a very long string and trying to find what part of the path is wrong.

Sub Open_DailyProd()

    Dim pathParts(1 To 10) As String
    Dim path As String

    pathParts(1) = "J:"
    pathParts(2) = "IAS"
    pathParts(3) = "3CMC05HA01"
    pathParts(4) = "IAC Clients"
    pathParts(5) = Sheet1.Cells(32, 2)
    pathParts(6) = Sheet1.Cells(11, 2)
    pathParts(7) = Sheet1.Cells(12, 2)
    pathParts(8) = Sheet1.Cells(13, 2)
    pathParts(9) = Sheet1.Cells(14, 2)
    pathParts(10) = Sheet1.Cells(6, 5) & Sheet1.Cells(15, 2) & ".xlsm"

    path = Join(pathParts, "\")

    Application.Workbooks.Open (path)

End Sub

Upvotes: 5

Related Questions