VBA to open file with specific name

Blunder1

Active Member
Joined
Jun 2, 2010
Messages
250
Hi all,

I have the below code which opens a particular workbook name depending on the date in cell a1. I wrote this the middle of last month and it works fine, however, i have found an error on this and am struggling to resolve.

Basically the file name it opens is in folder 2011, then folder July, then the file name is 'Raw Data' followed by the DDMM. Now we're at the beginging of the month this code bring 'thedate' up as 107 or 207 etc when the file names are 0107 or 0207 etc...

Code:
ws.Range("a1").NumberFormat = "YYYY"
theyear = ws.Range("a1").Text
ws.Range("a1").NumberFormat = "MMMMMMMMMMMMM"
themonth = ws.Range("a1").Text
ws.Range("a1").NumberFormat = "DDMM"
thedate = ws.Range("a1").Text
        Workbooks.Open Filename:= _
       "C:\Raw Data\" & theyear & "\" & themonth & "\Raw Data " & thedate & "", ReadOnly:=True

Thanks in advance

Blunder
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi all,

I have the below code which opens a particular workbook name depending on the date in cell a1. I wrote this the middle of last month and it works fine, however, i have found an error on this and am struggling to resolve.

Basically the file name it opens is in folder 2011, then folder July, then the file name is 'Raw Data' followed by the DDMM. Now we're at the beginging of the month this code bring 'thedate' up as 107 or 207 etc when the file names are 0107 or 0207 etc...

Code:
ws.Range("a1").NumberFormat = "YYYY"
theyear = ws.Range("a1").Text
ws.Range("a1").NumberFormat = "MMMMMMMMMMMMM"
themonth = ws.Range("a1").Text
ws.Range("a1").NumberFormat = "DDMM"
thedate = ws.Range("a1").Text
        Workbooks.Open Filename:= _
       "C:\Raw Data\" & theyear & "\" & themonth & "\Raw Data " & thedate & "", ReadOnly:=True

Thanks in advance

Blunder


try this
Code:
Dim sDate as string
sdte = Format(ws.Range("a1"), "YYYY") & "\" & Format(ws.Range("a1"), "mmmm") & "\Raw Data " & Format(ws.Range("a1"), "DDMM")
        Workbooks.Open Filename:= _
       "C:\Raw Data\" & sdte, ReadOnly:=True
 
Upvote 0
Yet another way...

Code:
Workbooks.Open Filename:= _
    "C:\Raw Data\" & Format(ws.Range("A1").Value, "yyyy""\""mmmm""\Raw Data ""ddmm"), ReadOnly:=True
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,234
Members
452,898
Latest member
Capolavoro009

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