Search code examples
excelvbaworksheet

Copy workbook and to paste below another workbook MS Excel


First I open a SubWorkbook than copy data from worksheet to update MainWorkbook (paste below):

The problem occur when I try to set variable for worksheet from the workbook I just open. It said: "Subscript out of Range".

Picture1

Picture2

What happen to it and how I can fix it, or is it wrong direction that I have to go from another way.

    Sub Data_Inbound()
        Dim mywb As Workbook
        Dim FName As String
        Dim wsCopy As Worksheet
        Dim wsDest As Worksheet
        Dim lCopyLastRow As Long
        Dim lDestLastRow As Long

        Set mywb = ActiveWorkbook
    
    On Error GoTo errHandler:
        FName = Application.GetOpenFilename(filefilter:="Excel Files,*.xlsx*", Title:="Please select an Excel file")
        Workbooks.Open FileName:=FName
    
         Set wsCopy = Workbooks(FName).Worksheets(Sheet1)
         Set wsDest = Workbooks(mywb).Worksheets(Sheet1)
         
         lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
         lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
            
        wsCopy.Range("A2" & lCopyLastRow).Copy _
        wsDest.Range("A" & lDestLastRow)
    
    wsDest.Activate
    
    errHandler:
  MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Data_Inbound"
        Exit Sub
        
    End Sub

I see other guy they have the same question, but they use worksheet name, for me I use a variant variable, but it lead to error: Other guy:

Set wsCopy = Workbooks("Warranty Template.xlsm").Worksheets("PivotTable")

Solution

  • Copy/Paste From Closed Workbook

    • The code has run successfully if you see the message. If not, something went wrong, and there is a message in the Immediate window CTRL+G.

    The Code

    Option Explicit
    
    Sub Data_Inbound()
            
        ' Initialize error handling.
        Const ProcName As String = "Data_Inbound"
        ' Do not use error handling while developing the code.
        On Error GoTo clearError ' Turn on error trapping.
            
        ' Define Destination Workbook.
        Dim wb As Workbook
        Set wb = ThisWorkbook ' The workbook containing this code.
        
        ' Define Source Workbook Name.
        Dim srcName As String
        srcName = Application.GetOpenFilename(filefilter:="Excel Files,*.xlsx*", _
                                              Title:="Please select an Excel file")
        
        ' Open Source Workbook (No variable, but it is the active one).
        Workbooks.Open Filename:=srcName
        
        ' Define Source Worksheet ('wsSource').
        Dim wsSource As Worksheet
        Set wsSource = ActiveWorkbook.Worksheets("Sheet1") ' Note the double quotes.
        
        ' Define Destination Worksheet ('wsDest')
        Dim wsDest As Worksheet
        Set wsDest = wb.Worksheets("Sheet1") ' Note the double quotes...
        ' ... and not: Set wsDest = Workbooks(wb).Worksheets("Sheet1") - wrong!
        
        ' Define Source Last (Non-Empty) Row ('srcLastRow').
        Dim srcLastRow As Long
        srcLastRow = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
        
        ' Define Destination First (Empty (available)) Row ('destFirstRow').
        Dim destFirstRow As Long
        destFirstRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
            
        ' Copy from Source to Destination.
        wsSource.Range("A2:A" & srcLastRow).Copy wsDest.Range("A" & destFirstRow)
        ' Note "A2:A" and not: "A2" - wrong!
        
        ' Now you wanna close the Source Workbook, but how?
        ' You can use the 'Parent' property:
        wsSource.Parent.Close False ' False means not to save changes.
        
        ' If you closed, wsDest is active again so you don't need:
        'wsDest.Activate
        
        ' Inform user, so you know the code has finished.
        MsgBox "Copied data.", vbInformation, "Success"
        
    ProcExit:
        Exit Sub
    
    clearError:
        Debug.Print "'" & ProcName & "': " & vbLf _
                  & "    " & "Run-time error '" & Err.Number & "':" & vbLf _
                  & "        " & Err.Description
        On Error GoTo 0 ' Turn off error trapping.
        GoTo ProcExit
            
    End Sub