Hello Everyone,
I have encounter a strange situation, the codes that i have below sometime runs without any error message, but
sometime it gives me a Run-Time Error '91': Object variable or With block variable not set
Can someone please help to explain why this happens and what can i do to resolve this situation?
It highlights : FoundVar=Range("B1:B" & LstRow).Find(what:=Var, LookIn:=xlValues).Row
Here is my code:
Dim Compare As String, LeftRes As String
Dim LstRow As Integer, Var As Integer, FoundVar As Integer, i As Integer, j As Integer
Range("B1").Value = "Item Code"
Range("D1").EntireColumn.Delete
Range("C1").Offset(0, 1).EntireColumn.Insert
Range("C1").Offset(0, 1).Value = "Cnt Qty"
LstRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1").Offset(0, 1).EntireColumn.Insert
Range("A1").Offset(0, 1).EntireColumn.NumberFormat = "@"
For i = 1 To LstRow
LeftRes = Left(Range("A" & i + 1), 4)
Range("B" & i + 1).Value = LeftRes
Next
Range("B2").Select
Var = "0201"
FoundVar = Range("B1:B" & LstRow).Find(What:=Var, LookIn:=xlValues).Row
For j = 1 To FoundVar
Compare = StrComp(Range("B2"), "0201")
If Compare <> 0 Then
ActiveCell.EntireRow.Delete
Else
Call PageFormat
Range("B1").EntireColumn.Delete
MsgBox "DONE !!", vbInformation, "Message Box"
Exit Sub
End If
Next
Thank you for your help in advance.
I have encounter a strange situation, the codes that i have below sometime runs without any error message, but
sometime it gives me a Run-Time Error '91': Object variable or With block variable not set
Can someone please help to explain why this happens and what can i do to resolve this situation?
It highlights : FoundVar=Range("B1:B" & LstRow).Find(what:=Var, LookIn:=xlValues).Row
Here is my code:
Dim Compare As String, LeftRes As String
Dim LstRow As Integer, Var As Integer, FoundVar As Integer, i As Integer, j As Integer
Range("B1").Value = "Item Code"
Range("D1").EntireColumn.Delete
Range("C1").Offset(0, 1).EntireColumn.Insert
Range("C1").Offset(0, 1).Value = "Cnt Qty"
LstRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1").Offset(0, 1).EntireColumn.Insert
Range("A1").Offset(0, 1).EntireColumn.NumberFormat = "@"
For i = 1 To LstRow
LeftRes = Left(Range("A" & i + 1), 4)
Range("B" & i + 1).Value = LeftRes
Next
Range("B2").Select
Var = "0201"
FoundVar = Range("B1:B" & LstRow).Find(What:=Var, LookIn:=xlValues).Row
For j = 1 To FoundVar
Compare = StrComp(Range("B2"), "0201")
If Compare <> 0 Then
ActiveCell.EntireRow.Delete
Else
Call PageFormat
Range("B1").EntireColumn.Delete
MsgBox "DONE !!", vbInformation, "Message Box"
Exit Sub
End If
Next
Thank you for your help in advance.