Avoid empty cells in a column Range

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
334
Office Version
  1. 2016
Platform
  1. Windows
I have a sheet where the user must enter a value in the range("A3:A12"). See enclosed image.

If he accidentally deletes one of these cells then VBA traps the error, see the code below.

But when he deletes MORE THAN ONE one cell then error 13 "type mismatch".

Any ideas ?


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim col_A_rng As Range
Set col_A_rng = Range("A3:A12")

If Target.Count > 0 And Not Application.Intersect(Target, col_A_rng) Is Nothing Then
   
    If Target.Value = "" Then
        Target.Select
        Application.EnableEvents = False
        MsgBox ("you must enter a value"), vbCritical
        Application.Undo
        Application.EnableEvents = True
    End If

End If

End Sub
 

Attachments

  • 230316.png
    230316.png
    13.4 KB · Views: 6
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Yes, because you're going to have multiple ranges. Try like that:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim col_A_rng As Range
Set col_A_rng = Range("A3:A12")

If Target.Count > 0 And Not Application.Intersect(Target, col_A_rng) Is Nothing Then
    If Target.Cells(1).Value = "" Then
        Target.Cells(1).Select
        Application.EnableEvents = False
        MsgBox ("you must enter a value"), vbCritical
        Application.Undo
        Application.EnableEvents = True
    End If
End If

End Sub
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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