Avoid empty cells in a column Range

robertvdb

Active Member
Joined
Jan 10, 2021
Messages
327
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: 4
Last edited by a moderator:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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