Swiching between 2 open workbooks

Tarkin

New Member
Joined
Feb 12, 2019
Messages
12
OK here is what I am trying to do


I have a workbook (lets call it A) which has a fixed name and stores the VBA codes.


The macro in A creates a workbook (B) which is saved under a name which varies.


Another macro in A does some processing on B. Ideally, I need a VBA subroutine to switch from A to B and vice versa to ensure macros are performed on the right workbook


Thanks in advance for any help.
 

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
Possibly, the things you want your macros to perform on workbook B don't need workbook B to be the active workbook. In that case you don't need to switch between workbooks. Switching takes time and slows the execution of your code. If you post your macros and explain what they are intended to do, perhaps someone here can help you adjust them so that switching is not necessary.
 
Upvote 0
Referring to workbooks in the code as below allows you to simply reference the data instead of switching to and from. You can now copy and paste to and from.

Code:
Sub Macro1()
  Dim TWB As Workbook
  Dim WB_B As Workbook
  Dim Sht1 As Worksheet
  Dim Sht2 As Worksheet
  Dim Rng1 As Range
  Dim Rng2 As Range
  
  Set TWB = ThisWorkbook
  Set WB_B = Workbooks("WorkbookB.xlsm")
  Set Sht1 = TWB.Sheets("Sheet1")
  Set Sht2 = WB_B.Sheets("Sheet2")
  Set Rng1 = Sht1.Range("A1")
  Set Rng2 = Sht2.Range("B2")




End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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