使用 CInt(Right()) 时出现不匹配错误
Mismatch Error when using CInt(Right())
我正在使用以下代码来识别电子表格中最后填充的行和随后的第一个空行,并使用此信息确定用什么填充空行的第一个单元格 (PC 1, PC 2、PC 3 等)。然而,当我尝试 运行 时,我总是收到 "type-mismatch" 错误,而且我不确定我需要更改什么来修复它。下面是有问题的代码,我在调试器停止的行正上方添加了注释。
'Creates variable for the next empty row and for PC #.
Dim lastRow As Long
Dim emptyRow As Long
Dim NewPC As Long
'Determines last row.
lastRow = Range("A65536").End(xlUp).Row
'Determines empty row.
emptyRow = Range("A65536").End(xlUp).Row + 1
'Determines PC entry value <--- DEBUGGER STOPS ON FOLLOWING LINE OF CODE.
NewPC = CInt(Right(Cells(lastRow, 1).Value, Len(Cells(lastRow, 1).Value) - InStrRev(Cells(lastRow, 1).Value, " "))) + 1
'Makes PC Data Sheet active.
Sheet1.Activate
'Enters PC # data into first column.
Cells(emptyRow, 1).Value = "PC" & " " & NewPC
知道了!这是更改后的行:
'Determines PC entry value
If lastRow = 1 Then
NewPC = 1
Else
NewPC = Right(Cells(lastRow, 1).Value, Len(Cells(lastRow, 1).Value) - InStrRev(Cells(lastRow, 1).Value, " ")) + 1
End If
看来您也得到了 space。怎么样:
NewPC = CInt(Mid(Cells(lastRow, 1).Value, InStrRev(Cells(lastRow, 1).Value, " ")))
您确定 Cells(lastRow, 1).Value
中的实际内容可以解释为 String
吗?如果是错误,对其进行字符串操作将无效
我正在使用以下代码来识别电子表格中最后填充的行和随后的第一个空行,并使用此信息确定用什么填充空行的第一个单元格 (PC 1, PC 2、PC 3 等)。然而,当我尝试 运行 时,我总是收到 "type-mismatch" 错误,而且我不确定我需要更改什么来修复它。下面是有问题的代码,我在调试器停止的行正上方添加了注释。
'Creates variable for the next empty row and for PC #.
Dim lastRow As Long
Dim emptyRow As Long
Dim NewPC As Long
'Determines last row.
lastRow = Range("A65536").End(xlUp).Row
'Determines empty row.
emptyRow = Range("A65536").End(xlUp).Row + 1
'Determines PC entry value <--- DEBUGGER STOPS ON FOLLOWING LINE OF CODE.
NewPC = CInt(Right(Cells(lastRow, 1).Value, Len(Cells(lastRow, 1).Value) - InStrRev(Cells(lastRow, 1).Value, " "))) + 1
'Makes PC Data Sheet active.
Sheet1.Activate
'Enters PC # data into first column.
Cells(emptyRow, 1).Value = "PC" & " " & NewPC
知道了!这是更改后的行:
'Determines PC entry value
If lastRow = 1 Then
NewPC = 1
Else
NewPC = Right(Cells(lastRow, 1).Value, Len(Cells(lastRow, 1).Value) - InStrRev(Cells(lastRow, 1).Value, " ")) + 1
End If
看来您也得到了 space。怎么样:
NewPC = CInt(Mid(Cells(lastRow, 1).Value, InStrRev(Cells(lastRow, 1).Value, " ")))
您确定 Cells(lastRow, 1).Value
中的实际内容可以解释为 String
吗?如果是错误,对其进行字符串操作将无效