How can I prevent empty cells when hitting the delete button

Krulle1000

New Member
Joined
Feb 25, 2016
Messages
15
Hello,

I use data validation with a list of values (--Select--;1;2;3) for all cells in range "B7:E1006".
The default value should be --select-- .
With the vba code below: If someone hits the delete button the cell will become empty but refills with --select--.
That works fine, but if 2 or more cells are selected and deleted, the cells become empty :(
Vba gives a 'type mismatch' error on line 'If IsEmpty(.Value) then'
How can I prevent empty cells when hitting the delete button or how can I refill all empty cells with my default value --Select-- ?


If Not (Application.Intersect(Target, Range("B7:E1006")) _
Is Nothing) Then
With Target
If Not .HasFormula Then
Application.EnableEvents = False

If IsEmpty(.Value) Then
location = .Address(RowAbsolute:=False, ColumnAbsolute:=False)
Sheet2.Range(location) = "'--Select--"
End If

Application.EnableEvents = True
End If
End With
End If

Thanks.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I would iterate through the cells in the Target range.

Code:
If Not (Application.Intersect(Target, Range("B7:E1006")) Is Nothing) Then
Application.ScreenUpdating = False
For Each rng In Target
     If Not rng.HasFormula Then
         Application.EnableEvents = False

         If IsEmpty(rng.Value) Then
             location = rng.Address(RowAbsolute:=False, ColumnAbsolute:=False)
             Sheet2.Range(location) = "'--Select--"
         End If

         Application.EnableEvents = True
     End If
Next rng
Application.ScreenUpdating = True
 
Upvote 0
Thanks a lot gsistek !!!
That does the trick. Indeed, I had to iterate through the cells in the Target range. Make sense :)
Now all of the data validation cells can't be set empty anymore.
Next and final step is to prevent a copy/paste or drag/drop in the data validation cells :confused:
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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