Home » excel » excel – If statement ignoring equality

excel – If statement ignoring equality

Posted by: admin May 14, 2020 Leave a comment

Questions:

Scenario: I have a Function that compares the data in 2 different arrays and writes a corresponding value to a worksheet.

Array1:

+-------+----------------+-----------------+
| Name1 | Current Level1 | Previous Level1 |
+-------+----------------+-----------------+
| ID1   | b              | c               |
+-------+----------------+-----------------+
| ID2   | f*             | g               |
+-------+----------------+-----------------+
| ID3   |                |                 |
+-------+----------------+-----------------+
| ID4   | e              | e               |
+-------+----------------+-----------------+

Array2:

+----+--------------+
| ID | Corresponder |
+----+--------------+
| a  | 1            |
+----+--------------+
| b  | 2            |
+----+--------------+
| c  | 3            |
+----+--------------+
| d  | 4            |
+----+--------------+
| e  | 5            |
+----+--------------+
| f* | 6            |
+----+--------------+
| g  | 7            |
+----+--------------+
| h  | 8            |
+----+--------------+
| i  | 9            |
+----+--------------+

Scenario: I am running a loop that reads the letters in the first array, find a corresponding value in the second array (like a Vlookup) and writes an array exactly like the first, but instead with the corresponding numbers).

output:

+-------+----------------+-----------------+
| Name1 | Current Level1 | Previous Level1 |
+-------+----------------+-----------------+
| ID1   | 2              | 3               |
+-------+----------------+-----------------+
| ID2   | 6              | 7               |
+-------+----------------+-----------------+
| ID3   |                |                 |
+-------+----------------+-----------------+
| ID4   | 5              | 5               |
+-------+----------------+-----------------+

Issue: I am running this code, but for some of the values , even though the comparison is positive, the loop still jumps the inner commands.

Code:

Function match_up_values(cleanoutputArray As Variant, matchArray As Variant, targetColumn As Integer, matchColumn As Integer)

    For loopvar1 = 2 To UBound(cleanoutputArray, 1)
        For loopvar2 = 2 To UBound(matchArray, 1)
            If CStr(cleanoutputArray(loopvar1, targetColumn + 1)) = CStr(matchArray(loopvar2, ratingsColumn)) And CStr(cleanoutputArray(loopvar1, targetColumn + 1)) <> "" Then ' some times, even if this if is true, it steps to the next loopvar2
                shtOutput1.Cells(loopvar1, targetColumn + 1) = matchArray(loopvar2, matchColumn + 1)
                shtOutput1.Cells(loopvar1, 1) = cleanoutputArray(loopvar1, 1)
                shtOutput1.Cells(loopvar1, 2) = cleanoutputArray(loopvar1, 2)
                Exit For
            End If
        Next loopvar2
    Next loopvar1

End function

Question: What could be causing this error?

How to&Answers:

This code is not the fix to your problem. However, it is a little tool that might help you with your issue:

Function stringcompare(strA As String, strB As String)
    If StrComp(strA, strB, vbTextCompare) = 0 Then stringcompare = "Text match (case insensitive)"
    If StrComp(strA, strB) = 0 Then stringcompare = "Perfect match"
    If Len(strA) <> Len(strB) Then stringcompare = "Length of strings not matching"
    If StrComp(Trim(strA), Trim(strB), vbTextCompare) = 0 Then stringcompare = "Text match (case insensitive) but with padding"
    If StrComp(Trim(strA), Trim(strB)) = 0 Then stringcompare = "Perfect match but with padding"
    If stringcompare <> "" Then Exit Function
    Dim i As Long
    For i = 1 To Len(strA)
        stringcompare = stringcompare & "('" & Mid(strA, i, 1) & IIf(Mid(strA, i, 1) = Mid(strB, i, 1), "'='", "'<>'") & Mid(strB, i, 1) & "') "
    Next
End Function

Using this in the right place at the right time will allow you to see what is causing the two strings to mismatch.

Answer:

You have a number of errors in your code which would no doubt cause this issue:

  • ratingsColumn is not referenced in the function header (probably your issue)
  • shtOutput1 is not referenced in the scope of the function (it might be a global variable though)

Another issue could potentially be non-printing characters within the values

The following is untested but should work. It also currently directly replaces the values within the InArray which may not work with your other code without tweaking…

Function InnerReplace(ByRef InArray As Variant, ByVal MatchArray As Variant, TargetCol As Long, MatchCol As Long, ReplaceCol As Long)
    Dim i As Long, j As Long, Str1 As String, Str2 As String
    For i = 2 To UBound(InArray, 1)
        Str1 = CleanStr(CStr(InArray(i, TargetCol + 1)))
        For j = 2 To UBound(MatchArray, 1)
            Str2 = CleanStr(CStr(MatchArray(j, MatchCol)))
            If Str1 = Str2 And Str1 <> "" Then
                InArray(i, TargetCol + 1) = MatchArray(j, ReplaceCol) 'Str2
'                shtOutput1.Cells(i, TargetCol + 1) = MatchArray(j, MatchCol + 1)
'                shtOutput1.Cells(i, 1) = InArray(i, 1)
'                shtOutput1.Cells(i, 2) = InArray(i, 2)
'                Exit For
            End If
        Next j
    Next i

End Function

Function CleanStr(ByVal Value As String, Optional Clean As String = False) As String
    Dim i As Long, NonPrint() As Variant: NonPrint = Array(127, 129, 141, 143, 144, 157)
    For i = LBound(NonPrint) To UBound(NonPrint)
        CleanStr = Replace(CleanStr, Chr(NonPrint(i)), "")      ' Replace non-printing characters
    Next i
    CleanStr = Replace(CleanStr, Chr(160), Chr(32))             ' Replace strange space character

    If Clean = True Then CleanStr = Application.WorksheetFunction.Clean(CleanStr)
End Function