Macro Command To Get Back to Workbook

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a macro that copies a worksheet in the current workbook to a new workbook and then saves that new workbook. What I want to do is to then go back to my original workbook, leaving the new one still open.

I know I need a command before the "Sheets("Full Quote Document").Activate" line to select my original workbook, but my problem is, I cannot hardcode the name of the workbook, because the user may save it under different names over time. So I want to have a way of recording the name of the original workbook at the start into a variable and then be shown how to then select the workbook using that variable.

Thanks,

MikeG



====================================
Dim wbNew As Workbook
ThisWorkbook.Worksheets("Full Quote For New").Copy

Set wbNew = ActiveWorkbook
With wbNew
.SaveAs Filename:=Range("Save_Name") & ".xlsx"
Application.DisplayAlerts = True
.Close
End With

Sheets("Full Quote Document").Activate
 

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.
You already have your answer in the code you posted...

You used this
Set wbNew = ActiveWorkbook
to create an object variable for the New Book..

Just do the same thing PRIOR to creating the new book..

Code:
Dim wbNew As Workbook, wbCur As Workbook
 
Set wbCur = ActiveWorkbook
ThisWorkbook.Worksheets("Full Quote For New").Copy

Set wbNew = ActiveWorkbook
With wbNew
.SaveAs Filename:=Range("Save_Name") & ".xlsx"
Application.DisplayAlerts = True
.Close
End With

Sheets("Full Quote Document").Activate 
 
wbCur.Activate


Or, even simpler...
There is a built in object variable that refers to the book containing the code...

ThisBook.Activate

ThisBook Refers to the book containing the code.


Hope that helps.
 
Upvote 0
You already have your answer in the code you posted...

You used this
Set wbNew = ActiveWorkbook
to create an object variable for the New Book..

Just do the same thing PRIOR to creating the new book..

Code:
Dim wbNew As Workbook, wbCur As Workbook
 
Set wbCur = ActiveWorkbook
ThisWorkbook.Worksheets("Full Quote For New").Copy

Set wbNew = ActiveWorkbook
With wbNew
.SaveAs Filename:=Range("Save_Name") & ".xlsx"
Application.DisplayAlerts = True
.Close
End With

Sheets("Full Quote Document").Activate 
 
wbCur.Activate


Or, even simpler...
There is a built in object variable that refers to the book containing the code...

ThisBook.Activate

ThisBook Refers to the book containing the code.


Hope that helps.

Thanks very much Jon - that's great.

MikeG
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

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