I'm trying to update hyperlinks in one Excel spreadsheet, using a Find in another sheet in the same workbook.
The problem occurs on j = c.Find(k).Row
, where I receive the message
"Runtime error '91:' Object variable or With block variable not set."
It doesn't give me any issues with the d = c.Find(m).Row
which looks like it's been set up identically.
I know nothing about error handling in VBA - I've just never had to use it - so maybe there's something wrong with the result of the search?
Sub HypFix()
Dim k As String
Dim m As String
Dim i As Long
Dim g As String
Dim d As String
Dim j As String
Dim c As Range
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'CHANGE SHEET NAMES BELOW AS NEEDED
Set c = Sheets("Tables").Range("A1:A15071")
For i = 4 To 337
If Sheets("Contents").Cells(i, "A").Value <> "" Then
k = Sheets("Contents").Cells(i, "A").Value
m = Right(Sheets("Contents").Cells(i, "A").Value, 255)
g = Sheets("Contents").Cells(i, "A").Value
If Len(Sheets("Contents").Cells(i, "A").Value) > 255 Then
d = c.Find(m).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & d, _
TextToDisplay:=g
ElseIf Len(Sheets("Contents").Cells(i, "A").Value) <= 255 Then
j = c.Find(k).Row
Sheets("Contents").Hyperlinks.Add Anchor:=Sheets("Contents").Cells(i, "A"), _
Address:="", _
SubAddress:="'Tables'!A" & j, _
TextToDisplay:=g
End If
End If
Next i
'Message Box when tasks are completed
MsgBox "Task Complete!"
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.CalculateFull
End Sub