运行时错误91对象variables或未设置块variables
我是新来的VBA为Excel和我坚持一个小问题。 我有一个数据列表,我有两个任务,我想完成。
第一个将删除J列中任何等于0的值的行;
第二部分:我想循环遍历剩下的行,并删除I列中的值大于J列中的值的行。
但是,我收到以下错误:VBA运行时错误91对象variables或未设置块variables
这是说:
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
只是改变:
For rw = .Cells(rows.Count, "E").End(xlUp).Row To 2 Step -1
至
For rw = .Cells(.rows.Count, "E").End(xlUp).Row To 2 Step -1
不同之处在于,当您从不同的工作表调用函数时, rows.count
不适用于同一工作表,因此它会rows.count
。
Dim rows2 As Range
.Cells(.rows.Count, "E").End(xlUp).Row
,
因为rows.count
会触发错误,因此rows.count
+1