I am working with find functions in VBA Excel, so when I ran into problems I pulled some example code from the help provided in Excel. I took their code that illustrates a basic
find function and pasted it into a macro. On running the macro, I get a “Runtime error ’91′” and the debugger highlights the line of code containing the angled brackets <>. These are the part of the code that I cannot understand.
Can anyone tell me what these brackets represent?
Sub exampleFindReplace()
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
The
<>operator meansc.AddressIs Not Equal TofirstAddress.In a C-style language this would be equivalent to
c.Address != firstAddress.Side note, I think you are getting error 91 (Object variable or With block variable not set.) because the line of code
Loop While Not c Is Nothing And c.Address <> firstAddresswill always try to execute the second condition (c.Address <> firstAddress) even if the first (While Not C Is Nothing) evaluates to false. Thus the call on c.Address will raise the exception.Try writing the code like this as it will not allow that to happen: