VBA < 运算符导致类型不匹配错误
VBA < Operator causes Type Mismatch Error
我正在使用以下代码根据两个标准隔离某些数据集,(1) 关键字(即用户 1); (2) 标称值
以下代码似乎适用于 Microsoft Excel 2013,但在 Microsoft Excel 2010 上抛出不匹配错误。如果你能提供帮助,那就太好了。
代码如下:
Sub FinalApprover()
lastRow = Sheets("Source").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
If Sheets("Source").Cells(i, 1).Value = "User 1" And ("Source").Cells(i, 2) < 50000 Then
Sheets("Source").Cells(i, 3).Value = "Final Approver"
End If
Next
End Sub
错误在您根据工作表进行测试的值范围内。
您很可能需要使用一种数据验证形式来确保第 1 列中有字符串值,第 2 列中有数值。
我相信稍微处理一下错误可能会对这种情况有所帮助。
运行 这个函数代替你的旧函数,如果再次抛出错误,这个函数将写出错误所在的行以及你正在与硬编码 String 进行比较的值"User 1" & Integer 50000 然后暂停执行。程序停止后,您将能够通过按“CTRL + G”在即时 window 中看到这些值。
Sub FinalApprover_New()
Dim r As Long, lLastRow As Long
Dim wsSource As Worksheet
Dim bDebug As Boolean
Set wsSource = ThisWorkbook.Sheets("Source")
With wsSource
'lLastRow = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
lLastRow = .Rows.Count
For r = 2 To lLastRow
' bDebug is used to attempt to compare the values ahead of time to check if an exception is thrown.
bDebug = (.Cells(r, 1).Value = "User 1" And .Cells(r, 2) < 50000)
If Err.Number <> 0 Then
Debug.Print "Row: " & r & " | " & "Value 1: " & .Cells(r, 1).Value & " | Value 2: " & .Cells(r, 2).Value
Debug.Assert 0 ' Hardcoded breakpoint
End If
If .Cells(r, 1).Value = "User 1" And .Cells(r, 2) < 50000 Then
.Cells(r, 3).Value = "Final Approver NEW"
Exit For
End If
Next
End With
End Sub
我正在使用以下代码根据两个标准隔离某些数据集,(1) 关键字(即用户 1); (2) 标称值
以下代码似乎适用于 Microsoft Excel 2013,但在 Microsoft Excel 2010 上抛出不匹配错误。如果你能提供帮助,那就太好了。
代码如下:
Sub FinalApprover()
lastRow = Sheets("Source").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
If Sheets("Source").Cells(i, 1).Value = "User 1" And ("Source").Cells(i, 2) < 50000 Then
Sheets("Source").Cells(i, 3).Value = "Final Approver"
End If
Next
End Sub
错误在您根据工作表进行测试的值范围内。 您很可能需要使用一种数据验证形式来确保第 1 列中有字符串值,第 2 列中有数值。
我相信稍微处理一下错误可能会对这种情况有所帮助。 运行 这个函数代替你的旧函数,如果再次抛出错误,这个函数将写出错误所在的行以及你正在与硬编码 String 进行比较的值"User 1" & Integer 50000 然后暂停执行。程序停止后,您将能够通过按“CTRL + G”在即时 window 中看到这些值。
Sub FinalApprover_New()
Dim r As Long, lLastRow As Long
Dim wsSource As Worksheet
Dim bDebug As Boolean
Set wsSource = ThisWorkbook.Sheets("Source")
With wsSource
'lLastRow = wsSource.Cells(Rows.Count, 1).End(xlUp).Row
lLastRow = .Rows.Count
For r = 2 To lLastRow
' bDebug is used to attempt to compare the values ahead of time to check if an exception is thrown.
bDebug = (.Cells(r, 1).Value = "User 1" And .Cells(r, 2) < 50000)
If Err.Number <> 0 Then
Debug.Print "Row: " & r & " | " & "Value 1: " & .Cells(r, 1).Value & " | Value 2: " & .Cells(r, 2).Value
Debug.Assert 0 ' Hardcoded breakpoint
End If
If .Cells(r, 1).Value = "User 1" And .Cells(r, 2) < 50000 Then
.Cells(r, 3).Value = "Final Approver NEW"
Exit For
End If
Next
End With
End Sub