循环直到错误424

时间:2017-08-16 07:44:23

标签: vba excel-vba loops excel

我现在已经花了几个小时研究这个问题,但到目前为止还没能让它发挥作用。

我有一些过滤数据,我想将第一个数据单元存储为变量以在循环中使用。但是,一旦我的循环删除了所有数据,它应该自行结束,我收到424错误,需要对象。

代码

Sub jjjjj()

Dim rng As Range

ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 9).Select

Set rnge = Selection

Do Until (rnge.value = 0)        
    Range("i1").Activate
    Selection.End(xlDown).Select
    Selection.EntireRow.Delete                
Loop

End Sub

错误行为Do Until (rnge = 0)

我试过了Do until (rnge.value = "")

除了取出value

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我认为您正在使用类似下面代码的内容,代码中的解释为注释:

Option Explicit

Sub jjjjj()

Dim FiltRng As Range
Dim Rng As Range, C As Range

' set the filtered range
Set FiltRng = ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible)

' loop through areas of Filtered range
For Each Rng In FiltRng.Areas
    ' loop through cells in column "I" of current area
    For Each C In Rng.Columns("I")
        If C.Value = 0 Then Exit Sub ' if value is 0 exit sub
        C.EntireRow.Delete
    Next C
Next Rng

End Sub