Set Workbook

mayday1

Board Regular
Joined
Oct 5, 2007
Messages
241
A kind soul helped me out with this the other day. Now I have a subsequent question: How do I set a variable to this value?
Here's what I'm trying to do:
Set MyFile = Workbooks(" & Range("B1") & " " & Range("C1") & ".xlsx")
so it would end up being:
Set MyFile = Workbooks(Donors April.xlsx)
-----


Here's the original question and the brilliant answer:
OK, so I have a drop down in cell A1 to select the year and another dropdown in B1 to select the first part of a filename and a third dropdown in C1 to select the second part of the filename

So if select...
A1 = 2019
B1 = Donors
C1 = April

...and the file I want to open is E:\ReportFolder\2019\Donors April.xlsx
What would be the VBA to open this file?

Below was the answer provided - works great for the original question.
Workbooks.Open ("E:\ReportFolder" & Range("A1") & "" & Range("B1") & " " & Range("C1") & ".xlsx")
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Not quite sure what you are after but you could introduce variables to the code like this.
Code:
Dim MyFile As Workbook
Dim strFileName As String
Dim strFolder As String
Dim strPath As String

    strFileName = Range("B1").Value & " " & Range("C1").Value & ".xlsx"

    strFolder = Range("A1").Value

    strPath = "E:\ReportFolder\" & strFolder & "\"

    Set MyFile = Workbooks(strPath & strFileName)
 
Upvote 0
You don't need the front " &

Code:
Set MyFile = Workbooks(Range("B1") & " " & Range("C1") & ".xlsx")
 
Upvote 0
Try this

Code:
    wBook = Range("B1").Value & " " & Range("C1").Value & ".xlsx"

 'Can be
    Workbooks.Open "E:\ReportFolder\" & Range("A1").Value & "\" & wBook  
    Set MyFile = ActiveWorkbook
    
'Or
    Workbooks.Open "E:\ReportFolder\" & Range("A1").Value & "\" & wBook  
    Set MyFile = Workbooks(wBook)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,169
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top