Refer to a workbook with partial name

Uhrlaff

New Member
Joined
May 17, 2019
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I am looking for succinct vba code to refer to workbooks by partial name, and copy data from one to the other.
In the process, the user needs to have both workbooks ALREADY OPEN. The code I have now works, but only if the files are named exactly as listed in the code. So in reality, the users will name the files with a version date included.

1. Existing code that works when filenames are matching exactly:
Workbooks("sa_extract.xlsx").Worksheets(1).Range("A1:AM5000").Copy _
Workbooks("SA Process.xlsm").Worksheets("data").Range("A1")

2. Proposed code with wildcard * that I wish would work:
Workbooks("sa_extract*.xlsx").Worksheets(1).Range("A1:AM5000").Copy _
Workbooks("SA Process*.xlsm").Worksheets("data").Range("A1")

thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
How about
VBA Code:
   Dim Wbk1 As Workbook, Wbk2 As Workbook, Wbk As Workbook
   
   For Each Wbk In Workbooks
      If Wbk.Name Like "sa_extract*" Then Set Wbk1 = Wbk
      If Wbk.Name Like "SA Process*" Then Set Wbk2 = Wbk
   Next Wbk
   Wbk1.Worksheets(1).Range("A1:AM5000").Copy Wbk2.Worksheets("data").Range("A1")
 
Upvote 0
Solution
Thank you, Fluff. this solution works correctly for me, and it's succinct!

Regards
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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