I have the following the Worksheet's module and it all works fine (i.e. when the value is changed, when a value is added where the cell was previously empty/null). However, the issue is when the value in the cell is deleted.
If there is a value in the field and it's deleted, the vOldValue holds a value but then I get an error if the vNewValue is now empty. I've highlighted where I'm stuck, I tried using IsEmpty, IsNull, = "" and no version seems to work. Hoping someone can help, thanks in advance.
If there is a value in the field and it's deleted, the vOldValue holds a value but then I get an error if the vNewValue is now empty. I've highlighted where I'm stuck, I tried using IsEmpty, IsNull, = "" and no version seems to work. Hoping someone can help, thanks in advance.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Range("B39", "B42"), Target) Is Nothing Then
Else
Exit Sub
End If
End Sub
Code:
Sub Worksheet_Change(ByVal Target As Range)
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Static blnAlreadyBeenHere As Boolean
'This piece avoid to execute Worksheet_Change again
If blnAlreadyBeenHere Then
blnAlreadyBeenHere = False
Exit Sub
End If
'Now, we will store the old and new value
Dim vOldValue As Variant
Dim vNewValue As Variant
'To store new value <===========================================================THIS IS WHERE THE ISSUE LIES
If IsEmpty(Target.Value) = True Then
vNewValue = "Deleted"
Else
vNewValue = Target.Value
End If
Debug.Print vNewValue
'Undo to retrieve old value
'To avoid new Worksheet_Change execution
blnAlreadyBeenHere = True
Application.Undo
'To store old value
vOldValue = Target.Value
Debug.Print vOldValue
'To rewrite new value
'To avoid new Worksheet_Change execution agein
blnAlreadyBeenHere = True
Target.Value = vNewValue
Debug.Print vOldValue, vNewValue
Dim X As Integer
Set wb = ThisWorkbook
ShtName = "Revision History"
If Target.Cells.Count > 1 Then Exit Sub
X = EndRow + 1
' OldVal = Target.Value
wb.Sheets(ShtName).Range("AA" & X).Formula = "=Now()" 'Revision Date
wb.Sheets(ShtName).Range("AB" & X).Value = ActiveSheet.Name 'Sheet that was changed
wb.Sheets(ShtName).Range("AC" & X).Value = Target.Address ' Cell Changed
wb.Sheets(ShtName).Range("AD" & X).Value = vOldValue ' Changed from
wb.Sheets(ShtName).Range("AE" & X).Value = vNewValue ' Changed To
wb.Sheets(ShtName).Range("AF" & X).Value = Environ("username") ' User that made change
With wb.Sheets(ShtName)
.Range("AA" & X).Copy
.Range("AA" & X).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub