Simple Copy Procedure returning error

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
250
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I want my macro to run through a range on a user-maintained sheet, and if it meets a criteria, to copy a worksheet with the name stored in the next cell from another open workbook, into this workbook.

Variables are defined and set as follows:

WBk2 = The workbook opened by the macro - working ok, it's open
Wbk1 = The workbook to copy sheets into
WS1 = A Sheet in Wbk1
CellA as a Range

My VBA looks like this:

VBA Code:
For Each CellA In WS1.Range("WSCopyData")

    If CellA.Value = "Sales" Then
    
        If DoesSheetExist(Wbk1, CellA.Offset(0, 1)) = False Then
    
            Wbk2.Sheets(CellA.Offset(0, 1)).Copy Before:=WS1
    
        End If

    End If
    
Next CellA

I'm getting a Type Mismatch error on my Copy line.

As always I've done something silly probably with my referencing - can you see it?


PS DoesSheetExist is one of my functions that returns False if the sheet doesn't already exist in Wbk1.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi,
What is WS1 ???
You could test:
Wbk2.Sheets(CellA.Offset(0, 1)).Copy Before:=Wbk2.Sheets("WS1")
 
Upvote 0
I changed it to this:

Rich (BB code):
For Each CellA In WS1.Range("WSCopyData")

    If CellA.Value = "Sales" Then
   
        If DoesSheetExist(Wbk1, CellA.Offset(0, 1)) = False Then
   
            Wbk2.Sheets(CellA.Offset(0, 1).Value).Copy Before:=WS1
   
        End If

    End If
   
Next CellA

And it worked fine.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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