在下面的代码中,我使用 Intersect
来选择匹配特定条件,而不是选择整个行。
作为学习目的,我尝试使用(偏移和调整大小)而不是(相交),但它提高了
Run-time error '1004':Application-defined or object-defined error
在范围 A:AF
中找到我的值
提前感谢有用的评论和回答。
Sub Union_Test()
Dim ws As Worksheet: Set ws = ActiveSheet
Dim lastR As Long: lastR = ws.Cells(Rows.Count, 1).End(xlUp).Row
Dim cel As Range, rng As Range, uRng As Range
Set rng = ws.Range("V3:V" & lastR)
For Each cel In rng
If cel.value = "Yes" Then
If uRng Is Nothing Then
Set uRng = cel
Else
Set uRng = Union(uRng, cel)
End If
End If
Next cel
'If Not uRng Is Nothing Then Intersect(uRng.EntireRow, ws.UsedRange).Select 'this works perfectly
If Not uRng Is Nothing Then uRng.Offset(, -21).Resize(, 32).Select 'This raising error
End Sub
最佳答案
另一个选项,创建从 A 列到 AF 列的 rng
,然后循环遍历其 .Rows
。
Set rng = ws.Range("A3:AF" & lastR)
Dim rw As Range
For Each rw in rng.Rows
If rw.Range("V1").Value = "Yes" Then 'This refers to V3, V4, V5, etc.
If uRng Is Nothing Then
Set uRng = rw
Else
Set uRng = Union(uRng, rw)
End If
End If
Next
关于excel - 使用(偏移和调整大小)而不是(相交),引发运行时错误 '1004' :Application-defined or object-defined error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71604842/