excel - 使用(偏移和调整大小)而不是(相交),引发运行时错误 '1004' :Application-defined or object-defined error

标签 excel vba offset

在下面的代码中,我使用 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/

相关文章:

excel - 在保存之前如何从 "Save As"输入框(而不是文件对话框)获取文件名?

vba - 你如何让一个变量成为你的事件单元格?

excel - 可以在 Excel 中的条件范围内使用 QUARTILE 函数吗?

excel - Powershell - 将 .XLS 文件转换为 .XLSX

excel - 初始化时从工作表向用户窗体添加图片

ios - 使用动画设置 setContentOffset 时,UITextView 大字体文本消失

javascript - 滚动时检测第一个顶部元素

excel - 如何根据值的频率为一行中的单元格着色?

c# - 使用 C# 将大量 CSV 数据插入 mysql 数据库的最快方法是什么?

angular - NGX 数据表 - 使用函数重置偏移量