dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,392
- Office Version
- 365
- 2016
- Platform
- 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]
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]