使用(偏移和调整大小)而不是(相交),引发 运行 时间错误“1004”:应用程序定义或对象定义的错误
using (Offset & Resize) instead of (Intersect),raising Run-time error '1004':Application-defined or object-defined error
在下面的代码中,我使用 Intersect
到 select 匹配特定条件而不是 select 整行。
作为学习目的,我尝试使用(偏移和调整大小)而不是(相交),但它提高了
Run-time error '1004':Application-defined or object-defined error
我在 range 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
我修改了代码,在IF Condtion
之后添加了Offset和resize为Urng
本身的值,然后就可以了。
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.Offset(, -21).Resize(, 32)
Else
Set uRng = Union(uRng, cel.Offset(, -21).Resize(, 32))
End If
End If
Next cel
If Not uRng Is Nothing Then uRng.Select 'Now This works
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
你可以使用你的代码(从问题中)获得你想要的,只需要替换它的最后一行:
If Not uRng Is Nothing Then uRng.Offset(, -21).Resize(, 32).Select
在不连续的范围内不起作用,具有:
If Not uRng Is Nothing Then Intersect(uRng.EntireRow, ws.Range("A:AF")).Select
最好(更有效)在单列中从每行一个单元格构建一个 Union
范围。对于这样大的范围(也就列而言),它会消耗更多 Excel 资源...
在下面的代码中,我使用 Intersect
到 select 匹配特定条件而不是 select 整行。
作为学习目的,我尝试使用(偏移和调整大小)而不是(相交),但它提高了
Run-time error '1004':Application-defined or object-defined error
我在 range 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
我修改了代码,在IF Condtion
之后添加了Offset和resize为Urng
本身的值,然后就可以了。
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.Offset(, -21).Resize(, 32)
Else
Set uRng = Union(uRng, cel.Offset(, -21).Resize(, 32))
End If
End If
Next cel
If Not uRng Is Nothing Then uRng.Select 'Now This works
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
你可以使用你的代码(从问题中)获得你想要的,只需要替换它的最后一行:
If Not uRng Is Nothing Then uRng.Offset(, -21).Resize(, 32).Select
在不连续的范围内不起作用,具有:
If Not uRng Is Nothing Then Intersect(uRng.EntireRow, ws.Range("A:AF")).Select
最好(更有效)在单列中从每行一个单元格构建一个 Union
范围。对于这样大的范围(也就列而言),它会消耗更多 Excel 资源...