107

The problem is that != does not work as a function in excel vba.

I want to be able to use

If strTest != "" Then instead of If strTest = "" Then

Is there another approach to do this besides !=?

My function to mimic != is

Sub test()

Dim intTest As Integer
Dim strTest As String

intTest = 5

strTest = CStr(intTest) ' convert

Range("A" + strTest) = "5"



    For i = 1 To 10
        Cells(i, 1) = i

        If strTest = "" Then
            Cells(i, 1) = i
        End If

    Next i


End Sub
3

4 Answers 4

166

Because the inequality operator in VBA is <>

If strTest <> "" Then
    .....

the operator != is used in C#, C++.

29

In VBA, the != operator is the Not operator, like this:

If Not strTest = "" Then ...
1
  • 6
    This is incorrect. Not is the logical inversion operator, which corresponds to ! in C-style languages.
    – Zev Spitz
    Commented Jan 19, 2017 at 21:55
7

Just a note. If you want to compare a string with "" ,in your case, use

If LEN(str) > 0 Then

or even just

If LEN(str) Then

instead.

6
  • 6
    I know to people somewhat new to VBA, this answer may seem weird, but this, believe it or not, is FAR more efficient than checking against <> "" Commented Jan 29, 2015 at 16:52
  • 8
    Fun fact to back this answer: Visual Basic and Pascal languages store strings with their length in the beginning and the content itself right after that. C-based and Java languages, on the other hand, do not store the length and have the '\0' (null) terminator to signal that the string ended. Because of that, getting the length in VBA is fast -- it's just reading an integer from memory -- and is slow in Java -- you need to iterate through the string. Commented Feb 8, 2016 at 19:40
  • 1
    @LimaNightHawk far more efficient? Can you elaborate on that please? I guess that most modern compilers would catch the pattern <> "" and produce the same p-code as Len(str).
    – Roland
    Commented Oct 4, 2017 at 15:28
  • @Roland This has to do with how VBA stores strings in memory. I'll let you Google for a fuller explanation, but in short, part of how strings are stored is that the first bytes store the length of the string, then the following bytes store the characters: [3][C][A][T]. An "empty" string has a [0] for the first bytes and checking the Len allows the code to just check and compare Integers. Additionally, when you do a = "" that second "" has to be allocated as its own string first in memory, then compared to your target string. Commented Oct 5, 2017 at 16:41
  • 1
    @Roland: That would be an easy optimization, but, apparently, the VBA compiler does not do it. Someone did a benchmark with the result that Len(str) > 0 is about twice as fast as str <> "" for 10 million iterations. That said, we are talking about extreme micro-optimization here (0.36 vs 0.72 seconds for 10 million iterations), so I'll definitely stick with the more readable str <> "".
    – Heinzi
    Commented Jul 11, 2019 at 15:49
2

Try to use <> instead of !=.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.