首页 > 解决方案 > 复制时 VBA 应用程序定义或对象定义错误

问题描述

我正在编写一个代码,它将数据从另一个工作簿复制到当前打开的工作簿。但是,我遇到了我一直在思考但无法找到原因的应用程序定义或对象定义错误。这就是为什么我决定在这里发布我的问题并希望得到一些帮助。请在下面找到我的代码:

Option Explicit

Sub copyDataSEA()

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Dim destWs As Worksheet
Set destWs = ThisWorkbook.Worksheets("Sheet1")

Dim Report As Workbook
Set Report = Workbooks.Open(Filename:="C:\Users\John\Documents\My Docs\Report 2020 EMEA_SEA_SA Region.xlsx")
Dim sourceWs As Worksheet
Set sourceWs = Report.Worksheets("Raw")
    
Dim lastRow As Long
lastRow = sourceWs.Range("A" & sourceWs.Rows.CountLarge).End(xlUp).Row

Dim outRow As Long
outRow = 2

'Copying Header of source
destWs.Rows(1).EntireRow.Value = sourceWs.Rows(1).EntireRow.Value

    Dim i As Long
    For i = 2 To lastRow
        If sourceWs.Cells(i, 6) = "INDONESIA" Or sourceWs.Cells(i, 6) = "MALAYSIA" Or sourceWs.Cells(i, 6) = "THAILAND" Or sourceWs.Cells(i, 6) = "VIETNAM" _
        Or sourceWs.Cells(i, 6) = "CAMBODIA" Or sourceWs.Cells(i, 6) = "MYANMAR" Or sourceWs.Cells(i, 6) = "PHILIPPINES" Or sourceWs.Cells(i, 6) = "SINGAPORE" Then
            destWs.Rows(outRow).EntireRow.Value = sourceWs.Rows(i).EntireRow.Value
            outRow = outRow + 1
        End If
    Next i

Report.Close False

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

我在这一行遇到错误

destWs.Rows(outRow).EntireRow.Value = sourceWs.Rows(i).EntireRow.Value

遇到错误时,outRow 的值为 25728,而 i 为 44246。源文档的列数为 74518。

标签: vba

解决方案


推荐阅读