Sheets.Add is targeting the wrong workbook

sjesiono

New Member
Joined
Jun 20, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I'm trying to update code that will copy data on sheets from one workbook to another if they don't exist. The listed Sub will do that when I run it on its own. However, when I try to use it indirectly, by calling it with another macro, it adds the new sheet to the end of Target.Workbook instead of the Destination.Workbook. It then gives me an error saying that the name is already in use, because it's trying to rename the sheet to the one that it's supposed to be copying. Any suggestions as to where I'm going wrong on this?

VBA Code:
Sub WSMove()
'   Copy over any tabs, except the Instruction tab, that aren't in the new workbook already
    Dim ws As Worksheet
    Dim nm As name
    Dim found
    Dim name
    For Each ws In Workbooks("Target.Workbook.xlsm").Worksheets
    found = False
    
    name = ws.name
    If name <> "Instructions" Then
        For Each nws In Workbooks("Destination.Workbook.xlsm").Worksheets
            If name = nws.name Then
                found = True
            End If
        Next nws
        
        If found = False Then
            Sheets.Add(After:=Workbooks("Destination.Workbook.xlsm").Sheets(Workbooks("Destination.Workbook.xlsm").Sheets.count)).name = name
            Workbooks("Target.Workbook.xlsm").Sheets(name).Cells.Copy
            Workbooks("Destination.Workbook.xlsm").Worksheets(name).Range("A1").PasteSpecial xlPasteValues
            Workbooks("Destination.Workbook.xlsm").Worksheets(name).Range("A1").PasteSpecial xlPasteColumnWidths
            Workbooks("Destination.Workbook.xlsm").Worksheets(name).Range("A1").PasteSpecial xlPasteFormats
            Workbooks("Destination.Workbook.xlsm").Worksheets(name).Protect Password:="workbook"
        End If
    End If
    Next ws
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What if you change this line:
VBA Code:
Sheets.Add(After:=Workbooks("Destination.Workbook.xlsm").Sheets(Workbooks("Destination.Workbook.xlsm").Sheets.Count)).name = name
to:
Code:
Workbooks("Destination.Workbook.xlsm").Sheets.Add(After:=Workbooks("Destination.Workbook.xlsm").Sheets(Workbooks("Destination.Workbook.xlsm").Sheets.Count)).name = name
 
Upvote 0
Solution
So, just some trimming:
VBA Code:
If found = False Then
    With Workbooks("Destination.Workbook.xlsm")
        .Sheets.Add(After:=.Sheets(.Sheets.Count)).name = name
        Workbooks("Target.Workbook.xlsm").Sheets(name).Cells.Copy
        .Worksheets(name).Range("A1").PasteSpecial xlPasteValues
        .Worksheets(name).Range("A1").PasteSpecial xlPasteColumnWidths
        .Worksheets(name).Range("A1").PasteSpecial xlPasteFormats
        .Worksheets(name).Protect Password:="workbook"
    End With
End If
 
Upvote 0
Would this be easier?

VBA Code:
Workbooks("Target.Workbook.xlsm").Sheets(name).Copy After:=Workbooks("Destination.Workbook.xlsm").Sheets(Workbooks("Destination.Workbook.xlsm").Sheets.Count)
 
Upvote 0

Forum statistics

Threads
1,215,750
Messages
6,126,665
Members
449,326
Latest member
asp123

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