How to make same macro run on workbooks with different names

LoriD

Board Regular
Joined
Apr 1, 2002
Messages
148
Hello,

I have tried researching previous posts and can not seem to find anything similar to what I'm asking. I tried recording a macro to automate some tasks I perform every day between two workbooks. While in one I want to go get data from the other. The problem I'm having is the name of the workbooks I want to run this on changes every time I download my data into Excel. The files always start with the same name but the date gets appended to it. Is there a way to modify the macro to just work with the two books I've got open? Any help would be greatly appreciated!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
this is some code that I use to open two files and do work on

Code:
sFilename = Application.GetOpenFilename("XLS Files(*.xls),*.xls")
Set wb = Workbooks.Open(sFilename)
sName = ActiveSheet.Name
tFilename = Application.GetOpenFilename("Text Files(*.xls),*.xls")
Set WB2 = Workbooks.Open(tFilename)
tName = ActiveSheet.Name
 
Upvote 0
Thank you for your quick response. The code works great to find my files, but once I've got them open and attempt to run a macro, it has the original filenames in it. I was hoping there was a way to get those specific file names out of it so the macro will just run on whatever two workbooks I've got open.
 
Upvote 0
Can you post some of your code? What you want to do is replace the workbook references in the recorded code with references to WB and WB2 which you assign to the workbooks you are using.

If you have the workbooks already open then you can set WB and WB2 by looking at the workbooks collection and looking for workbooks with names that match a pattern.


The way that you use the WB references will depend a bit on what the code is trying to do.
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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