为什么我问两个变量是否相等的语句不起作用


Private Sub CommandButtonUndo_Click()
Dim Answer As Integer, LastRow As Variant, PreviousLastRow As Variant
LastRow = Worksheets("DATA").Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
PreviousLastRow = Names("PreviousLastRow").Value
If PreviousLastRow = LastRow Then
MsgBox "Can only undo Once"
Else
If LastRow > 1 Then
Answer = MsgBox("Are you sure you want to Undo the Previous Input?", vbYesNo + vbQuestion, "New Job")
If Answer = vbYes Then
Worksheets("DATA").Rows(LastRow).ClearContents
Names("PreviousLastRow").Value = LastRow - 1
End If   
End If 
End If
End Sub

该代码的思想是阻止用户多次单击撤消按钮。这让我感到困惑的具体部分是If PreviousLastRow = LastRow Then,它似乎被忽略了,并且总是遵循其他路径,即使我知道这两个变体是相同的

这是因为代码看不到这两个数字相同吗?

来自Name.Value文档:

返回或设置一个String值,该值表示定义名称要引用的公式。

PreviousLastRow = Names("PreviousLastRow").Value

这返回=10,所以去掉=CLng的结果:

PreviousLastRow = Names("PreviousLastRow").Value
PreviousLastRow = Replace(PreviousLastRow, "=","")
PreviousLastrow = Clng(PreviousLastRow)

最新更新