我正在尝试同时比较2个字符串值-帐户编号和帐户名称。我已将所有变量明确设置为字符串,但是在IF比较中仍然出现不匹配错误。
有人知道我在做什么错吗?
Dim i As Integer
For i = 2 To 10
Dim accountNumber As String
accountNumber = CStr(Sheet1.Cells(i, 2).Value)
Dim accountName As String
accountName = CStr(Sheet1.Cells(i, 3).Value)
Dim c As Integer
c = 4
Dim j As Integer
For j = 2 To 14831
Dim accountNumber2 As String
accountNumber2 = CStr(Sheet2.Cells(j, 2).Value)
Dim accountName2 As String
accountName2 = CStr(Sheet2.Cells(j, 3).Value)
If (accountNumber = accountNumber2 & accountName = accountName2) Then
Dim platform As String
platform = Sheet2.Cells(j, 11).Value
Sheet1.Cells(i, c).Value = platform
c = c + 1
End If
Next j
Next i
End Sub
正如@ cybernetic.nomad在您的问题下方的注释中所述,您应该使用and
运算符,该运算符是逻辑和,而不是&
,对于字符串来说,这意味着串联。
写作时
accountNumber = accountNumber2 & accountName = accountName2
然后&
的优先级高于=
,所以这实际上是什么意思:
accountNumber = (accountNumber2 & accountName) = accountName2
所以发生了什么:
accountNumber2
和accountName
产生一个字符串。accountNumber
与导致布尔值的串联结果进行比较。accountName2
进行比较,这可能不是您想要的。