运行此代码时返回VBA运行时错误1004“应用程序定义或对象定义的错误”
。我搜索了谷歌/ Stack Overflow,他们都告诉我指定范围,但有趣的是,我指定范围后发生错误。 (在修改之前,它确实有效,但返回了错误的结果)
Worksheets(2).Range(Cells(finderA.Row, rng2.Column), Cells(finderA.Row, rng2.Columns.Count + rng2.Column - 1)).Copy finalWS.Cells(rowCounter, rng1.Columns.Count + 2)
- >这是行返回错误。
通过比较具体的共同价值,整个代码是我检查两个数据之间遗漏的长线的一部分。
我在这段代码中基本上要做的是将一个范围循环到另一个范围并按顺序复制粘贴匹配的行(即,我正在从表1,2中制作sheet3。)
Dim rowCounter As Integer
rowCounter = 1
Dim eachCellA As Range, eachCellB As Range
Dim eachCellAa As Range, eachCellBb As Range
Dim StrFirstAdd As String, StrAdd As String
Dim finderA As Range, finderB As Range
For Each eachCellA In addCellRngA
With addCellRngB
Set finderA = .Find(eachCellA.Value, LookIn:=xlValues, Lookat:=xlWhole)
End With
If Not finderA Is Nothing Then
eachCellA.EntireRow.Copy finalWS.Cells(rowCounter, 1)
StrFirstAdd = finderA.Address
StrAdd = finderA.Address
Do
Worksheets(2).Range(Cells(finderA.Row, rng2.Column), Cells(finderA.Row, rng2.Columns.Count + rng2.Column - 1)).Copy finalWS.Cells(rowCounter, rng1.Columns.Count + 2) **##--> Debugger stops here, returns error.**
Set finderA = addCellRngB.FindNext(finderA)
StrAdd = finderA.Address
rowCounter = rowCounter + 1
Loop While Not finderA Is Nothing And StrAdd <> StrFirstAdd
Else
eachCellA.EntireRow.Copy finalWS.Cells(rowCounter, 1)
rowCounter = rowCounter + 1
End If
Next
这是代码,所有值(finderA.Row,rng2.Column,rng2.Columns.count等)都不是空的并且具有正确的值。
我认为粘贴整行是不必要的,但如果你觉得原因不在代码中,请告诉我,我会非常乐意提供整个代码。
答案 0 :(得分:3)
替换
With Worksheets(2)
.Range(.Cells(finderA.Row, rng2.Column), .Cells(finderA.Row, rng2.Columns.Count + rng2.Column - 1)).Copy finalWS.Cells(rowCounter, rng1.Columns.Count + 2) ' **##--> Debugger stops here, returns error.**
End With
与
eachCellA.EntireRow.Copy finalWS.Cells(rowCounter, 1)
您必须限定所有范围引用,包括单元格,否则后者可能引用不同的工作表(运行代码时以活动为准)。
你也可以从“如果你这样做”中删除这一行
Loop While StrAdd <> StrFirstAdd
并将循环条件更改为
$(this)