Subscription out of range when trying to copy worksheet data into existing workbook

Sevreral issues here:

  1. The Destination Workbook may or may not be open. If it’s not, open it
  2. The Destination Workbook may or may not have a sheet called “TAC data”.
  3. wsDest is a Worksheet, not a Workbook, so you need specify a Worksheet (as in your comment)
  4. When referencing an open Workbook, only specify the book name, without the path
Dim fPath as String
'...
fPath = "C:\TACs\"
fName = "ResumoTACs_" & Format(Date, "MM-YYYY") & ".xlsx"
fExists = Dir(fPath & fName)
'...
If fExists = "" Then
    '...
Else
    On Error Resume Next
    Set wbDest = Workbooks(fName) ' now that fName contains only the file name
    On Error GoTo 0
    If wbDest Is Nothing Then
        'Open it
        Set wbDest = Workbooks.Open(fPath & fName)
    End If
End If
'...
On Error Resume Nest
Set wsDest = wbDest.Worksheets("TAC data")
On Error GoTo 0
If wsDest Is Nothing Then
    ' Sheet missing.  What now?
Else
    '...

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top