Hank
Hank

Reputation: 187

VBA Program does not give the correct text when statement is false

My program is suppose to see if the length of the text is within 1 to 31.

If the length is not within 1 to 31 it will return a "FALSE" at e28.

My problem is no matter the length of the text, it always returns a "TRUE" please enlighten me on why this is happening.

x = Range("b26").Value

count = Len(x) - Len(Replace(x, "-", ""))

If Len(x) - count > 1 & Len(x) - count < 32 Then
    MsgBox Len(x) - count
    Range("e28").Value = "TRUE"
Else
    Range("e28").Value = "FALSE"
End If

Upvotes: 1

Views: 32

Answers (1)

Bathsheba
Bathsheba

Reputation: 234695

You need to use And rather than & in your If condition.

& is the string concatenation operator in VBA, and is not appropriate here.

(The fact that True evaluates to -1 in VBA also doesn't help matters, and & also has a different precedence to And.)

Upvotes: 3

Related Questions