Excel VBA比较if语句中的变体

时间:2020-07-17 12:29:26

标签: excel vba if-statement compare variant

让我解释一下我的问题: 我有两个Excel工作表“数据”和“输出”。在每张纸的第一列中,有一些数字需要比较。数字看起来像:3081671 如果我在工作表输出中找到一个与工作表数据中当前迭代匹配的数字,我希望工作表数据中同一行中的值写在工作表输出中的数字旁边。这实际上有效。 但是,工作表数据中的一些数字如下所示:V3081671A 迭代并在工作表数据中找到这样的数字时,我剪切了V和A使得只剩下整数。

但是,对于这些情况,即使test和comp应该包含完全相同的字符串,if语句“ If test = comp then ...”也是假的。为什么会这样?

我们非常感谢您提供的解决问题的帮助。如果我对它的描述不够清楚,请随时询问。

Sub MatchNumbers()
    Dim i As Integer
    Dim j As Integer
    Dim buffer As Variant
    Dim comp As Variant
    Dim test As Variant
    Dim inp As Variant


    j = 2
    While IsEmpty(ActiveSheet.Cells(j, 1)) = False
        ActiveSheet.Cells(j, 2) = "NV"
        j = j + 1
    Wend
    
    i = 2
    inp = Tabelle1.Cells(i, 1)
    While IsEmpty(inp) = False
        If Not IsNumeric(Left(inp, 1)) And Len(inp) >= 9 Then
            comp = Mid(inp, Len(inp) - 7, 7)
        Else
            comp = inp
        End If
    
        j = 2
        test = ActiveSheet.Cells(j, 1)
        Do While IsEmpty(test) = False
                If Left(inp, 1) = "V" Then
                    MsgBox ("Neue Zeile!")
                    ActiveSheet.Cells(j, 1).EntireRow.Insert Shift:=xlDown
                    j = j + 1
                    ActiveSheet.Cells(j, 1) = inp
                End If
                ActiveSheet.Cells(j, 2) = Tabelle1.Cells(i, 3)
                Exit Do
            End If
            j = j + 1
            test = ActiveSheet.Cells(j, 1)
        Loop
        i = i + 1
        inp = Tabelle1.Cells(i, 1)
    Wend
End Sub

0 个答案:

没有答案
相关问题