Variable workbook declaration

hammy77

New Member
Joined
Nov 27, 2019
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hello
I have a workbook that contains data and a VBA to do some tidying up and formatting. I need to be able to open up 2 other workbooks to do a vlookup on, the problem I have is the 2 workbooks change name everyday. How do I declare these other 2 in my main workbook.

Any suggestions would be grateful
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What logic is there to those 2 workbooks? e.g. a filename containing today's date, the most recent file in folder x, ...
 
Upvote 0
Record a macro while opening one of those workbooks. The recorder will give you something like this:
Code:
    Workbooks.Open Filename:= _
        "https://SomePathToYourSharepoint/Book1 26.11.2019.xlsx"
Modify that to:
Code:
Workbooks.Open Filename:= _
"https://SomePathToYourSharepoint/Book1 " & Format(Date, "dd.mm.yyyy") & ".xlsx"
Repeat for the other one, making a similar change to the recorded code.
 
Upvote 0
Thank you, That sounds straight forward. This may be a silly question, I have done this but it looks for today's date, how can I do this for other dates. And how would I be able to reference these with a workbook.activate. I apologise if I don't understand
 
Upvote 0
So how would you know which date to use?
To later refer to an opened workbook in your code:
Code:
Dim Wb1 as Workbook
Set Wb1 = Workbooks.Open("Filenamegoeshere")
'Other code
'Now to address the new workbook, you use Wb1 and to address the workbook containing the code you use "ThisWorkbook".
'For instance, this copies A1:A10 from sheet Sheet1 to that same sheet in the workbook which contains the code
Wb1.Worksheets("Sheet1").Range("A1:A10").Copy
ThisWorkbook.Worksheets("Sheet1").Range("A1").PasteSpecial xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,153
Members
452,615
Latest member
bogeys2birdies

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