VBA to open workbook

SAT-65

Board Regular
Joined
Feb 10, 2008
Messages
69
I'm trying to use a code to open a saved workbook. In a worksheet (Drop Page) A1 is the Month, A2 is the Day, and A3 is the year. When I change these values it is to change the name of the workbook I'm trying to open. For some reason it can't find the saved workbook. I can open the workbook manually with no issues. This is the popup I'm getting. Any help would be GREAT.

1713978790169.png


Sub FindSheet_and_open()

Dim wb As Workbook
Dim ws As Worksheet
Dim MainPath As String
Dim Month As String
Dim Day As String
Dim Year As String
Dim FullPath As String


Set wb = ThisWorkbook
Set ws = wb.Worksheets("Drop Sheet")

MainPth = "R:\Production\Production Reports\"
Month = ws.Range("B1")
Day = ws.Range("B2")
Year = ws.Range("B3")

FullPath = MainPath & Month & "-" & Day & "-" & Year & ".xlsm"

Workbooks.Open (FullPath)


End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I am not sure if it is your only problem or not, but you should NEVER use reserved words (words of existing functions, properties, methods, objects, etc) as the names of your user defined variables.
Month, Day, and Year are all names of pre-existing functions. Using them can cause unexpected results and errors.
Try choosing different variable names.
 
Upvote 0
After your FullPath calculation, add a message box to see what the exact value is:
VBA Code:
MsgBox FullPath

Then verify that a file with that exact name exists in the exact file path.
And make sure that the extension matches too.
 
Upvote 0
It should be showing the file path too.
Where is that?
 
Upvote 0
You have a misspelled variable name that is causing problems.

VBA Code:
MainPth = "R:\Production\Production Reports\"

Should be

VBA Code:
MainPath = "R:\Production\Production Reports\"

To avoid that kind of error in the future, put an Option Explicit statement at the top of your code module.
 
Upvote 0
Solution
You have a misspelled variable name that is causing problems.

VBA Code:
MainPth = "R:\Production\Production Reports\"

Should be

VBA Code:
MainPath = "R:\Production\Production Reports\"

To avoid that kind of error in the future, put an Option Explicit statement at the top of your code module.
Good catch!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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