VBA - Open excel file in relative path containing month

Josu

New Member
Joined
Mar 2, 2021
Messages
39
Office Version
  1. 2010
Platform
  1. Windows
Hello, I tried to google hardly, but can't get macro working, always as month showing January
This is code I have

VBA Code:
Sub test3()

Dim dt As String, dt2 As String
Dim filename As String, strMonth As String

strMth = MonthName(Month(Now()))
If Month(Now()) = 1 Then
    mth = "1"
    strPriorMth = MonthName(12, True)
Else
    mth = Month(Now() - 1)
    strPriorMth = MonthName(Month(Now()) - 1, True)
End If

dt = Format(mth, "mm")
dt2 = Format(mth, "mmmm")

filename = "C:\GENERAL\2023\" & dt & " " & dt2 & "\" & "Handover*.xlsm"

Debug.Print filename
'Workbooks.Open filename
End Sub
File which I want to open will be in "C:\GENERAL\2023\07 July\ or either another month
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
The issue is that you are trying to apply date formatting to a number (and not a date).

Try this:
VBA Code:
Sub test3()

Dim dt As String, dt2 As String
Dim filename As String, strMonth As String
Dim mth As Byte
Dim strMth As String, strPriorMth As String

strMth = MonthName(Month(Now()))
If Month(Now()) = 1 Then
    mth = 1
    strPriorMth = MonthName(12, True)
Else
    mth = Month(Now() - 1)
    strPriorMth = MonthName(Month(Now()) - 1, True)
End If

dt = Format(DateSerial(2023, mth, 1), "mm")
dt2 = Format(DateSerial(2023, mth, 1), "mmmm")

filename = "C:\GENERAL\2023\" & dt & " " & dt2 & "\" & "Handover*.xlsm"

Debug.Print filename
'Workbooks.Open filename
End Sub
 
Upvote 0
Solution
The issue is that you are trying to apply date formatting to a number (and not a date).

Try this:
VBA Code:
Sub test3()

Dim dt As String, dt2 As String
Dim filename As String, strMonth As String
Dim mth As Byte
Dim strMth As String, strPriorMth As String

strMth = MonthName(Month(Now()))
If Month(Now()) = 1 Then
    mth = 1
    strPriorMth = MonthName(12, True)
Else
    mth = Month(Now() - 1)
    strPriorMth = MonthName(Month(Now()) - 1, True)
End If

dt = Format(DateSerial(2023, mth, 1), "mm")
dt2 = Format(DateSerial(2023, mth, 1), "mmmm")

filename = "C:\GENERAL\2023\" & dt & " " & dt2 & "\" & "Handover*.xlsm"

Debug.Print filename
'Workbooks.Open filename
End Sub
Thanks a lot, working as I wanted
Is it any way to modify a bit this outcome to detect year?
And one more thing, any way to say for example if 08 August don't exist, open 07 July?
I am very new with vba :(
 
Upvote 0
Is it any way to modify a bit this outcome to detect year?
If you are referring to the "2023" in my calculation, note that it does not matter what year you put in there.
We are just trying to create a valid date so we can apply the Format function to it and pull the month value off.

Note, however, you can easily get the year of the current date like this:
VBA Code:
yr = Year(Now())

And one more thing, any way to say for example if 08 August don't exist, open 07 July?
That is really a whole different question, and as such, should be posted to a new question.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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