MZimmerman6
MZimmerman6

Reputation: 8593

Check if cell was empty Excel VBA

I am writing an Excel Macro to display a warning if the user edits a cell that was previously empty. Basically if a cell is edited, is in column 1, and already contains text I want to display a warning But if it does not have text already, I do not want to.

So what I have tried is the following

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 1 And Not IsEmpty(Cells(Target.Row,Target.Column)) Then
        Application.EnableEvents = False
        MsgBox "Some Message"
        Application.EnableEvents = True
    End If
End Sub

The issue I am having is when I am getting the cell to see if it was empty or not, it will never return that it was because, well the user just added stuff to it.

So I want to know if there is a simple way to check the previous state of that cell. I want to find out if the cell WAS empty. Is this possible?

Upvotes: 1

Views: 9569

Answers (1)

Declan_K
Declan_K

Reputation: 6826

You could use the Worksheet_SelectionChange event to capture the value of the cell that the user has selected in a variable, which happens before they make any changes.

Then when Worksheet_Change is fired, you can refer to that variable.

Upvotes: 4

Related Questions