【问题标题】:If statement ignoring equality如果语句忽略相等
【发布时间】:2019-09-16 12:51:02
【问题描述】:

场景:我有一个比较两个不同数组中的数据并将相应值写入工作表的函数。

数组1:

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

数组2:

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

场景:我正在运行一个循环,它读取第一个数组中的字母,在第二个数组中找到对应的值(如 Vlookup)并写入一个与第一个数组完全相同的数组,但是和相应的数字)。

输出:

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

问题:我正在运行这段代码,但是对于某些值,即使比较是肯定的,循环仍然会跳转内部命令。

代码:

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

问题:什么可能导致此错误?

【问题讨论】:

  • 您是否曾尝试在工作表中:=A1=A2 查找您认为应该匹配的单元格?通常有一个尾随或前导空格会引发这种比较。
  • 是的,@Luuklag 有道理。尝试在阅读单元格时添加Trim
  • @Luuklag我试过了,结果是假的。我将两者都复制并粘贴到文本编辑器中,它们是相同的。
  • @Tom 是的。错误只是当我将问题转移给 SO 时。
  • 你怎么称呼Function?为什么你使用Function而不是Sub,因为你没有返回任何值?

标签: excel vba


【解决方案1】:

此代码不能解决您的问题。但是,它是一个可以帮助您解决问题的小工具:

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

在正确的位置在正确的时间使用它可以让您了解导致两个字符串不匹配的原因。

【讨论】:

    【解决方案2】:

    您的代码中有许多错误无疑会导致此问题:

    • 函数头中没有引用ratingsColumn(可能是你的问题)
    • shtOutput1 在函数范围内没有被引用(虽然它可能是一个全局变量)

    另一个问题可能是值中的非打印字符

    以下内容未经测试,但应该可以使用。它目前还直接替换 InArray 中的值,如果不进行调整,这些值可能无法与您的其他代码一起使用...

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-11
      • 2015-11-27
      相关资源
      最近更新 更多