Reputation: 27
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
The idea of the code is to stop a user being able to click the undo button more than once. The specific part this is tripping me up is If PreviousLastRow = LastRow Then
which just seems to be ignored and always follows the else path, even when I know the two variants are the same
Is this to do with the two numbers not being seen the same by the code?
Upvotes: 0
Views: 65
Reputation: 50008
From the Name.Value
documentation:
Returns or sets a String value that represents the formula that the name is defined to refer to.
PreviousLastRow = Names("PreviousLastRow").Value
This returns =10
, so get rid of the =
and CLng
the result:
PreviousLastRow = Names("PreviousLastRow").Value
PreviousLastRow = Replace(PreviousLastRow, "=","")
PreviousLastrow = Clng(PreviousLastRow)
Upvotes: 2