复制工作簿并粘贴到另一个工作簿下方 MS Excel

Copy workbook and to paste below another workbook MS Excel

首先我打开一个子工作簿,然后从工作表复制数据以更新主工作簿(粘贴在下面):

当我尝试为刚打开的工作簿中的工作表设置变量时出现问题。它说:“下标超出范围”。

它发生了什么,我该如何解决它,或者我必须从另一个方向走的方向是错误的。

    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

我看到其他人也有同样的问题,但他们使用的是工作表名称,我使用的是变体变量,但它会导致错误: 其他人:

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

Copy/Paste 来自已关闭的工作簿

  • 如果您看到该消息,代码已 运行 成功。如果不是,就是出错了,Immediate window CTRL+G.
  • 有消息

代码

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