I am trying to run the following code but I keep getting the error "Run-time error 91: Object variable or With block variable not set"
numberOfRows = Application.CountA(Range("A:A"))
Application.CutCopyMode = False
Dim numberOfRemainingDuplicates As Integer
Dim lastOne As Variant
Dim workingRange As Variant
numberOfRemainingDuplicates = numberOfRows - 1
lastOne = "A1"
workingRange = "AF3:AF" & numberOfRows
Do
Range(workingRange).Select
Selection.Find(What:="1", After:=ActiveCell, LookIn:=xlValues, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
lastOne = ActiveCell.Address
ActiveCell.EntireRow.Select
Selection.Insert Shift:=xlDown
numberOfRows = numberOfRows + 1
Range(lastOne).Select
Selection.Offset(2, 0).Select
workingRange = ActiveCell.Address & ":$AF$" & numberOfRows
numberOfRemainingDuplicates = Application.CountA(Range(workingRange))
Loop While numberOfRemainingDuplicates > 1
(I highlighted in brown the portion of the code that gets highlighted when I enter debugging mode)
I have tried to introduce workingRange as
Dim workingRange As Range
and then removing the Range functions from workingRange in the rest of the code, but then I get other errors. What confuses me the most is that before I saved and closed the workbook that contains this macro, I ran the macro once successfully, so I find it strange that this error would only appear now?
I hope someone can see what is wrong with this code?
Bookmarks