vba-excel meaning of <> (angled brackets or greater-than and less-than symbols)

22,391

The <> operator means c.Address Is Not Equal To firstAddress.

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 <> firstAddress will 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:

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)
        If c Is Nothing Then Exit Do
    Loop While c.Address <> firstAddress
End If
End With

End Sub
Share:
22,391

Related videos on Youtube

Charlie
Author by

Charlie

I am a software developer happily living in Minneapolis, Minnesota.

Updated on July 01, 2020

Comments

  • Charlie
    Charlie almost 4 years

    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
    
  • Charlie
    Charlie almost 13 years
    Thanks, I didn't know what to call them, so they were hard to search for. I imagine having a search for "vba operators" would have helped, but I couldn't think to call it that. This helps a lot.
  • Matt
    Matt almost 13 years
    @Charlie, Your welcome. By the way, on this site (seeing you are new) if an answer is helpful you upvote it (click the up arrow), and if it solves your question, you Accept it (click the checkmark). I'm happy to help nonetheless.
  • Admin
    Admin almost 13 years
    As far as why c.Address <> firstAddress is always evaluated: VBA does not support short-circuit boolean operators :-) Not sure why, but that's just the way it is.
  • Matt
    Matt almost 13 years
    @pst, Yep, if this was VB.NET the short-circuit operator would have been AndAlso.
  • Charlie
    Charlie almost 13 years
    @Matt Spinelli Thanks Matt! I was a little uncertain about the distinction. I've still got to earn my upvote priveleges though, so once I get there I'll be much more discriminate with m positive feedback to helpful people.
  • Charlie
    Charlie almost 13 years
    @Matt Spinelli The code in your answer works great! I was looking around other places to try and fix my find functions, because I was sure that must already have been answered somewhere. This has helped that a ton and saved me a lot of time.