【问题标题】:Copy workbook and to paste below another workbook MS Excel复制工作簿并粘贴到另一个工作簿下方 MS Excel
【发布时间】:2020-10-02 04:04:06
【问题描述】:

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

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

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

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

【问题讨论】:

  • 按 F8 单步执行代码,直到出错,然后更新您的问题,更具体地说明您的期望。
  • 我在链接中添加了图片错误描述,这就是我看到错误的地方。
  • Set wsCopy = Workbooks(FName).Worksheets(Sheet1) 更改为Set wsCopy = Workbooks(FName).Worksheets("Sheet1") 看看会发生什么或指定Sheet1 是什么
  • mywb 是包含此代码的工作簿吗?
  • 是的,我将它用于活动工作表

标签: excel vba worksheet


【解决方案1】:

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

  • 如果您看到该消息,则代码已成功运行。如果不是,则出现问题,并且立即窗口中有一条消息 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

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    相关资源
    最近更新 更多