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
731
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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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