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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

gsistek

Well-known Member
Joined
Apr 4, 2011
Messages
660
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
 

Krulle1000

New Member
Joined
Feb 25, 2016
Messages
15
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:
 

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
8,611
Office Version
  1. 2016
Platform
  1. Windows
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,707
Messages
5,833,242
Members
430,199
Latest member
Petty queen

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
Top