VBA to get part of the path and part of the filename to open a file

SandsB

Well-known Member
Joined
Feb 13, 2007
Messages
736
Office Version
  1. 365
Platform
  1. Windows
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?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Code:
 Workbooks.Open ("E:\ReportFolder\" & Range("A1") & "\" & Range("B1") & " " & Range("C1") & ".xlsx")
 
Upvote 0
How about
Code:
Sub SandsB()
   Dim Pth As String, Fname As String
   
   Pth = "E:\ReportFolder\" & Range("A1").Value & "\"
   Fname = Range("B1").Value & " " & Range("C1").Value & ".xlsx"
   Workbooks.Open Pth & Fname
End Sub
 
Upvote 0
Thank you.
People like you have helped me so many times here.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
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