String testing in VBA returns wrong result

Joined
Jul 23, 2012
Messages
6
Hello,
I am working on a function that should compare a String
Code:
originalName
to a list of names. I use a While loop with an if statement checking at every line of the list whether or not the content is identical to the string in argument.
However, when I run the function step by step, when I arrive at the line where the two strings are identical, the comparison still deems them as false and the loop continues to the end of the list.

The originalName is extracted from this list so it shouldn't have any extra spaces that would make the comparison return false. I have good notions of VBA, yet I can't find the mistake I made :-/
I attach my code for this function below,

thanks in advance

Code:
Public Function TrouverNom(originalName As String) As Integer
    Dim i As Integer
    Sheets("3-Planning par monteurs IT").Activate
    i = 7
    Dim presence As Boolean
    presence = False
    'Looping on the list of names, firstblank3 is a module variable indicating
    'the first blank cell on this particular list.
    While (presence = False And i <= firstblank3)
        'The error is at the line below
        If Range("B" & i).Value = originalName Then
        'The error is at the line above
        presence = True
        End If
        i = i + 1
    Wend
    TrouverNom = i - 1
    Sheets("2-Besoin chantier").Activate
End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Maybe this

Code:
Public Function TrouverNom(originalName As String) As Long
Dim Found As Range
Set Found = Sheets("3-Planning par monteurs IT").Columns("B").Find(what:=originalName, LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
    TrouverNom = 0
Else
    TrouverNom = Found.Row
End If
End Function
 
Upvote 0
Are you sure the case is exactly the same?
 
Upvote 0
Hello RoryA,
I checked it was exactly the same, and there is no difference in the case (All names are in format SURNAME Firstname, and the name I am comparing to the list originates in the list)
 
Upvote 0
Hello VoG,
thank you for your answer,
I tested your code, at execution the program finds Found is nothing, could it have anything to do with the fact that xlValues is at -4163 in my module?
 
Upvote 0
Does it work if you use:
Code:
Public Function TrouverNom(originalName As String) As Integer    Dim i As Integer
    Dim presence As Boolean
    i = 7
    presence = False
    'Looping on the list of names, firstblank3 is a module variable indicating
    'the first blank cell on this particular list.
    While (presence = False And i <= firstblank3)
        'The error is at the line below
        If Sheets("3-Planning par monteurs IT").Range("B" & i).Value = originalName Then
        'The error is at the line above
        presence = True
        End If
        i = i + 1
    Wend
    TrouverNom = i - 1
End Function
 
Upvote 0
Then the data does not match. If you alter the code to:
Code:
Public Function TrouverNom(originalName As String) As Integer    Dim i As Integer
    Dim presence As Boolean
    i = 7
    presence = False
    'Looping on the list of names, firstblank3 is a module variable indicating
    'the first blank cell on this particular list.
    While (presence = False And i <= firstblank3)
        'The error is at the line below
        Debug.Print "Input: " & originalName & vbLf & "Output: " & Sheets("3-Planning par monteurs IT").Range("B" & i).Value
        If Sheets("3-Planning par monteurs IT").Range("B" & i).Value = originalName Then
        'The error is at the line above
        presence = True
        End If
        i = i + 1
    Wend
    TrouverNom = i - 1
End Function
then you will be able to see the values in the Immediate Window in the VBEditor to see what comparison is occurring.
 
Upvote 0
The immediate window showed that there was an extra space which I hadn't noticed!
I am terribly sorry, especially since I said I had carefully checked!:rolleyes:

Thanks a lot for your time and all your answers, you've been a great help! :)
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,462
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top