Question : Mandatory Range of Cells in Excel

I would like to make a range of cells mandatory for a value (not be blank) in excel.
I tried the attached code , but it will not work for a range of cells.  If I just change the range to only A1 it works fine.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
 
Set c = Range("A1:A10000")
 
 
Application.EnableEvents = False
 
If c.Text = " " Then
Application.Goto reference:=c, scroll:=True
MsgBox ("Field Cannot Be Blank")
End If
 
Application.EnableEvents = True
End Sub

Answer : Mandatory Range of Cells in Excel

Ok, so the SelectionChange event only knows the cell that is currently selected, not the previous selection.  Also, "skipping" a cell by just pressing Enter or Tab is not detectable via any event I know of.  You have to actually enter the cell in order to use Data Validation so that is not an option either.

I recommend just moving the user to the first blank cell.  You can do this by either changing the reference for the goto to cel instead of c.

WC
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c As Range
Dim cel As Range
 
Set c = Range("A1:A10000")
 
 
Application.EnableEvents = False
If Not Intersect(Target,c) Is Nothing Then 
  For each cel in c
    If cel.Text = " " Then
      Application.Goto reference:=cel, scroll:=True
      MsgBox ("Field Cannot Be Blank")
      Exit For
    End If
  Next cel
End If 
Application.EnableEvents = True
End Sub
Random Solutions  
 
programming4us programming4us