我是新來的VBA excel,我堅持一個小問題。我有一個數據列表,我有兩個任務要完成。運行時錯誤91對象變量或塊變量未設置
第一個將刪除J列中任何等於0的值的行;
第二部分:我想遍歷剩餘的行並刪除I列中的值大於值J列中的任何行。
不過,我發現了以下錯誤:VBA運行時錯誤91對象變量或帶塊變量未設置
這是各國行:
For rw = .Cells(rows.Count, "E").End(xlUp).Row To 2 Step -1
完整的代碼
Private Sub clearupdata_Click()
Dim rows As Range, OSQty As Range, value As Long, rw As Long
Set OSQty = Range("j2")
Do Until OSQty.value = ""
value = Val(OSQty.value)
If (value = 0) Then
If rows Is Nothing Then
Set rows = OSQty.EntireRow
Else
Set rows = Union(OSQty.EntireRow, rows)
End If
End If
Set OSQty = OSQty.Offset(1)
Loop
If Not rows Is Nothing Then rows.Delete
With Worksheets(1) '<~~ worksheet ~~>
For rw = .Cells(rows.Count, "E").End(xlUp).Row To 2 Step -1
If .Cells(rw, "I").Value2 > .Cells(rw, "J").Value2 Then
.rows(rw).EntireRow.Delete
End If
Next rw
End With
End Sub
使用'rows'作爲一個Range對象變量名是不是一個好主意因爲barrowc表示已經在應用程序,工作表和範圍對象 – barrowc
上定義了'Rows'屬性,rows.count可能會認爲它是名爲rows的範圍變量。我嘗試重命名它的工作範圍 –