Search code examples
vbaexcelexportexport-to-excel

Trying to copy Range from worksheet into a new workbook. Excel VBA


Here is what I have:

Sub exportDataAM()
Dim wbS As Workbook, wbT As Workbook
Dim wsS As Worksheet, wsT As Worksheet

Set wbS = ThisWorkbook 'workbook that holds this code
Set wsS = wbS.Worksheets("Recap").Range("A1:R5")

wsS.Copy
Set wbT = ActiveWorkbook 'assign reference asap

Set wsT = wbT.Worksheets("Recap").Range("A1:R5")
wsT.Name = "Recap" 'rename sheet

wbT.SaveAs wbS.Path & "\" & "Recap AM" & Format(Now, " dd-mm-yyyy ") 'save new workbook
End Sub

I get a Type mismatch which just doesn't make sense to me regarding the .Range on Line 6. I know there are other ways to do this, but this is working fine without the .Range and I wanted to figure out specifically why that may be.

Any help on this will be greatly appreciated.


Solution

  • You have declared the variables wsS and wsT as Worksheets. Yet, you try adding a range into the variable. This will cause a mismatch.

    Either declare the variables as Range, or add only the worksheet into the variable.