Iluvpresident
Iluvpresident

Reputation: 175

Case-Insensitive String Comparison

I'm trying to find a way to show the MsgBox if those two strings are equal but doesn't look at upper- or lowercase.

For teller = 1 To 51
If Cells(teller, 5).Value = Me.txtGebruikersnaam Then
    MsgBox ("Deze Gebruiker bestaat al!")
    Exit Sub
    Else
End If
Next

I tried to use:

If (StrComp(Cells(teller, 5).Value = Me.txtGebruikersnaam, vbTextCompare) = 0) Then

but it didn't work.

Upvotes: 0

Views: 2242

Answers (2)

Ben
Ben

Reputation: 355

You actually have a subtle bug in your usage of StrComp.

The code in the original post states:

If (StrComp(Cells(teller, 5).Value = Me.txtGebruikersnaam, vbTextCompare) = 0) Then

The problem is that you have a typo, and meant to write:

If StrComp(Cells(teller, 5).Value, Me.txtGebruikersnaam, vbTextCompare) = 0 Then

The subtlety is that Cells(teller, 5).Value = Me.txtGebruikersnaam will evaluate to False/0 unless you have a perfect case sensitive match, and then get compared to vbTextCompare, which is a predefined constant with value 1.

Try it with a comma rather than an equals.

PS: Also removing extra brackets as pointed out by Excel Hero.

Upvotes: 4

Excel Hero
Excel Hero

Reputation: 14764

Please try:

If LCase$(Cells(teller, 5).Value) = LCase$(Me.txtGebruikersnaam) Then

Upvotes: 5

Related Questions