Open a workbook with a reference to a date in a cell

Mange

New Member
Joined
Sep 8, 2016
Messages
20
Dear all

I tried to create a code that will open a workbook. I don't want to use the exact file location, but instead, I want to work with a reference to a date in a particular cell.

So e.g. I need to open a file called 'Test_20160630_V2'. Because I will need to do this every month, I don't want to copy/paste the exact file location every month, but I want to include a reference to the particular date, in this case 30/06/2016.

Code:
Sub Test()

Dim Test As Worksheet
Dim wbB As Workbook
Dim YYYY As String
Dim MM As String
Dim DD As String
Dim DatReport As Range

Set DatReport = ActiveWorkbook.Worksheets("Test").Range("B2")

YYYY = Format(DatReport, "yyyy")
MM = Format(DatReport, "mm")
DD = Format(DatReport, "dd")

Set wbB = Workbooks.Open("F:\TestMcTest\Test_report\YYYY\YYYYMMDD\2_TestieTest\Test_YYYYMMDD_V2.xls")

End Sub

So YYYY returns 2016, MM 06 and DD 30.. Do I need to convert it to string, because Excel doesn't find the file.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Yeah. For example:

Code:
YYYYMMDD = YYYY & MM & DD
myFile = "F:\TestMcTest\Test_report\" & YYYY & "\" & YYYYMMDD & "\2_TestieTest\Test_" & YYYYMMDD & "_V2.xls"
Set wbB = Workbooks.Open(myFile)
 
Upvote 0
At the MyFile line.

Code:
Sub Test()

Dim Test As Worksheet
Dim wbB As Workbook
Dim YYYY As String
Dim MM As String
Dim DD As String
Dim YYYYMMDD As String
Dim DatReport As Range
Dim myFile As Workbook

Set DatReport = ActiveWorkbook.Worksheets("Test").Range("B2")

YYYY = Format(DatReport, "yyyy")
MM = Format(DatReport, "mm")
DD = Format(DatReport, "dd")

YYYYMMDD = YYYY & MM & DD
 
myFile = "F:\Risk_controlling\CALM_report\" & YYYY & "\" & YYYYMMDD & "\2_calculation\Switcher_" & YYYYMMDD & "_v0.1.xls"
Set wbB = Workbooks.Open(myFile)

End Sub
 
Upvote 0
myFile isn't a workbook. Its a string. The workbook is wbB. Change your dim statements.
 
Upvote 0
Hah indeed.. It works now, thanks!

Could you just explain why you need to code it like this: " & YYYYMMDD & " ?
 
Upvote 0
Its because that is a variable. If you kept it in the string excel would just look for the folder called literally YYYYDDMM rather than the string represented by the variable. So to excel it would be searching for this folder:

"F:\Risk_controlling\CALM_report\YYYY\YYYYMMDD"

when you actually wanted:

"F:\Risk_controlling\CALM_report\2016\20160630"
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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