Run macro if cell is left blank

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
354
Office Version
  1. 2007
Platform
  1. Windows
Hello,

I’m working on small issue where if a cell value is edited and inadvertently left blank, I want to run a macro that returns the previous value that was edited/deleted when the user leaves the “empty” cell (presumably by Enter or Arrow). I only want This to apply to cells in range(“M33:M2033”). Any suggestions would be appreciated.

Thanks for viewing,
Steve
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This will only work if only 1 cell was changed. Not if you copy and paste a blank cell into a whole bunch of cells. Does this work for you?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim d As Range
Set d = Intersect(Target, Range("M33:M2033"))
If d Is Nothing Then Exit Sub
If d.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
    If d = "" Then Application.Undo
Application.EnableEvents = True
End Sub
 
Upvote 0
Solution
This will only work if only 1 cell was changed. Not if you copy and paste a blank cell into a whole bunch of cells. Does this work for you?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim d As Range
Set d = Intersect(Target, Range("M33:M2033"))
If d Is Nothing Then Exit Sub
If d.Cells.Count > 1 Then Exit Sub
Application.EnableEvents = False
    If d = "" Then Application.Undo
Application.EnableEvents = True
End Sub
Thank you Scott for your quick response. This appears to be doing exactly as I desired. I'll have to do more testing which may be a bit of an issue as my primary laptop just got sick (the one I'm using/borrowing is less than desirable). I'll keep you posted but this really looks good.

Again, much appreciated,
Steve K.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top