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

问题描述

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

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

Picture1

Picture2

它发生了什么以及如何修复,或者我必须从另一种方式走错方向。

    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")

解决方法

从封闭的工作簿复制/粘贴

  • 如果看到此消息,则代码已成功运行。如果没有,则出现问题,并且“立即”窗口 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...