First I opened this question: Check merged cell and compare adjacent to set unique value from compared cells values. Same scenario, Key value at column 6
and value at column 31
but now requirements have changed.
Now I need to search every cell in column 6
and for each occurrence of this search, copy the the value (column 25
) from the first occurrence to the others. I mean, if I have five results of searching "KEY_VALUE", I need to copy the value in column 25
associated with the first result of the search to the others results in their correspondent cells in column 25
.
Problem is, when I run my code I get run time error 91: object variable or with block variable not set even when I do have a With block.
I was reading Object variable or with block not set - but in my case, I'm trying to assign a string variable with the result of the search not a range. Here's my code:
Sub CopyUUID()
Dim lRow As Long
Dim rng As Range
Dim searchResult As Range
Dim ws As Worksheet
Dim strSearch As String
Dim uuid As Variant
Set ws = Sheets(ActiveSheet.Name)
With ws
lRow = .Range("F" & .Rows.count).End(xlUp).row
Set rng = .Range(.Cells(3, 6), .Cells(lRow, 6))
For Each cellchecked In rng.Cells
If Not IsEmpty(cellchecked.Value) Or Not cellchecked.Value = "" Then
'strSearch = cellchecked.Value
Set searchResult = rng.Find(What:=cellchecked.Value, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
uuid = searchResult.Offset(0, 25).Value 'Gives error "run time error 91: object variable or with block variable not set"
Do
Set searchResult = rng.FindNext(After:=searchResult)
If Not searchResult Is Nothing Then
searResult.Offset(0, 25).Value = uuid
Else
Exit Do
End If
Loop
Else
'If cell is blank, skips execution as no continue exists in VBA
End If
Next cellchecked
End With
ExitProgram:
Exit Sub
End Sub
What could be the problem?