Dacromir
Dacromir

Reputation: 228

How do I check if cell contents match a specific string?

I'm building a quick Excel Macro that iterates through a raw input range, row by row, looking for different text values (which it sends to other worksheets as needed). I'm running into a problem with my If statements.

Code snippet of the affected area

For i = 1 To (maxRow - 1)

    If IsEmpty(myRange.Cells(i, 3)) Then

        'Do things

    ElseIf myRange.Cells(i, 3).Value = "Company Meeting" Then

        'Do other things

    End If

Next

The code inside the ElseIf never runs. I changed the ElseIf line to

ElseIf myRange.Cells(i, 3).Value = 3 Then

...and added a 3 to the column we're looking at in the input, and it ran the code inside ElseIf. This tells me that my code is looking at the right columns and the right cells. For some reason, the issue only happens if my ElseIf is looking for a string value - if I try to check for a number I don't see any issues.

I've also tried the following:

ElseIf myRange.Cells(i, 3).Text = "Company Meeting" Then
ElseIf myRange.Cells(i, 3) = "Company Meeting" Then

...with the same results

How can I check if the text in the cell matches a specific string with an If statement?

Upvotes: 0

Views: 2848

Answers (1)

Harry
Harry

Reputation: 163

Agree with Scott Craner, I have had issues dealing with whitespace characters within excel. Copy and paste the contents of the cell into notepadd++ and then enable hidden chars with something like:

  • View->Show Symbol->Show White Space and TAB

This may help you identify exactly what you are looking at within you cell!

Upvotes: 1

Related Questions