What is the code to activate a previous workbook

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,375
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have some code that copies a worksheet and pastes it into a new document but I want to add in something to activate the sheet called "Home" in the file that the sheet was created from, once the new document has been finished with. This will mean that once the document is closed, the home sheet will be activated instead of the sheet that was copied. What is wrong with this code as it will not work. It says subscript out of range. It highlights this line of code Workbooks("wb").Worksheets("home").Activate

CODE]Private Sub cmdCopySheet_Click()

Dim CopyMonth As String
Dim wb As Workbook
Set wb = ThisWorkbook
CopyMonth = Range("J13").Value

Worksheets(CopyMonth).Activate
Worksheets(CopyMonth).Columns("A:E").Select
Call AddNew
Workbooks("wb").Worksheets("home").Activate

Sub AddNew()
Dim xWs As Worksheet
Dim Rng As Range
Set Rng = Application.Selection
Application.Workbooks.Add
Set xWs = Application.ActiveSheet
Rng.copy Destination:=xWs.Range("A1")
End Sub

End Sub[/CODE]
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I would think it should look like this:
Dim wb As Workbook
Set wb = ThisWorkbook
wb.Worksheets("home").Activate


This will activate the current workbook sheet not the new workbook sheet.

You never named the new Workbook or named the new workbook sheet.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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