VBA code to switch back and forth between 3 different workbooks

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
169
Office Version
  1. 365
Hi,

Wondering if anyone could help me with a problem I have with switching between 3 different open workbooks.

The Workbook names never remain constant.

1st workbook ( Main Workbook that the code is ran from ) name will always change
2nd workbook , name will change but will always have "Test1" included in the name
3rd workbook , name will change but will always have "Test2" included in the name

I need to toggle between each workbook to copy and paste data.

Any help is greatly appreciated
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How are you opening the 2nd and 3rd workbooks?
 
Upvote 0
Hi Norie , The 2nd and 3rd workbooks are already open , they are not opened through the code.

And that's the problem I have been having , I cant figure out how to do it as they are already opened.
 
Upvote 0
Hi ,

Just refreshing this post with a comment hoping someone can assist. Would anyone have any suggestions?

Thanks
 
Upvote 0
Hi ,

Just refreshing this post with a comment hoping someone can assist. Would anyone have any suggestions?

Thanks

Will there be other workbooks open aside the 3 workbooks ? BTW, you don't need to activate workbooks in order to copy from or paste to them.

It would be useful if you could show us some small code example that illustrates what and how you are copying and pasting
 
Upvote 0
Hi , There is the possibility that other workbooks will be open yes but for the macro I will only be using the 3 of them.

So I will be running the macro when Test3 is the active workbook , then I want to copy data (as below) into the Test3 workbook from Test1 & 2.

The problem I am having is that the workbook names for Test1 & 2 will not remain constant bar the Test1 and Test2 piece.

Windows("111111_1111111_Test1.csv").Activate
Selection.Copy
Windows("Test1.xls").Activate
Range("E2601").Select
ActiveSheet.Paste
 
Upvote 0
try something like this (untested)
Code:
Sub test()
Dim wbname As String
Dim wb As Workbook
Dim wbs As Workbooks
Set wbs = Application.Workbooks
wbfnd = False
For Each wb In wbs
   wbname = wb.Name
   If InStr(wbname, "Test1") Then
    wb.Activate
    wbfnd = True
   End If
Next wb


End Sub
 
Last edited:
Upvote 0
Wouldn't it be easier if you asked the user to select the 2 other workbooks to open via a dialog?

If you did that you could create references to them which could be used throughout the rest of the code.
 
Upvote 0
Thanks everyone for their input, I have it working now. Here is my code


Sub BAU_Tool1()
Dim wbname As String
Dim wb As Workbook
Dim wbs As Workbooks
Dim w As Workbook: Set w = ActiveWorkbook

Set wbs = Application.Workbooks
wbfnd = False
For Each wb In wbs
wbname = wb.Name
If InStr(wbname, "DlyWorkings") Then
wb.Activate
wbfnd = True

Range("T2").Select
Selection.Copy
w.Activate
Sheets("Calculation").Select
Range("E2601").Select
ActiveSheet.Paste


End If
Next wb
Call BAU_Tool2
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,310
Members
449,152
Latest member
PressEscape

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