I am new to VBA for excel and I am stuck with a little problem. I have a list of data and I have two task I want to complete.
The first will delete rows with any values in the J column that is equal to 0;
The second part: I want to loop through the rows left over and delete any rows that the value in I column is greater than the value In J column.
However I'm getting the following error: VBA RunTime Error 91 Object variable or With block variable not set
This is on the line that states:
For rw = .Cells(rows.Count, "E").End(xlUp).Row To 2 Step -1
The full code is
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
rowsas a variable name for a Range object is not a good idea because there is already aRowsproperty defined on the Application, Worksheet and Range objects