Avoid empty cells in a column Range

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
339
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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