Solved

Excel help to define a mandatory cell

Posted on 2007-03-29
5
1,362 Views
Last Modified: 2008-01-09
I'm using Microsoft office 2003 and in excel I want to know if there is a way to make a field mandatory before moving to the next field kind of like it is done in Access.
0
Comment
Question by:centurian102
  • 4
5 Comments
 
LVL 17

Expert Comment

by:gtgloner
ID: 18816767
Paste this code in Sheet1 (or whatever sheet you want this to happen) of the VBA environment window. This will prompt the user to enter a value in Cell A1 if another cell is selected with A1 being empty:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Range("A1").Value = "" Then
  MsgBox ("You must enter a value for this cell")
 End If
End Sub
0
 
LVL 17

Expert Comment

by:gtgloner
ID: 18817022
Here is a slight improvement on my previous code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Range("A1").Value = "" Then
   Range("A1").Select
   MsgBox ("You must enter a value for Cell A1")
 End If
End Sub
0
 

Author Comment

by:centurian102
ID: 18817223
Can I choose this for multiple ranges? e.g. D9 through D15, I9 through I16, D20 through D26 and I20 through I26.
0
 
LVL 17

Expert Comment

by:gtgloner
ID: 18818403
Here is how to do it for your first range in a sheet. I haven't figured out how to get this to work for multiple ranges in the same sheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For i = 9 To 15
        If Range("D" & i).Value = "" Then
         Range("D9:D15").Select
        End If
    Next i
End Sub
0
 
LVL 17

Accepted Solution

by:
gtgloner earned 125 total points
ID: 18818445
If a cell within this range is empty and the user tries to select a cell outside of this range, the range will be autoselected and remain so until the entire range is filled with some kind of character(s). To navigate through this range, the user has to click the Enter key until he gets to the cell that has nothing in it, then fill it with something. After all cells within this range have at least one character in it, the user will be able to navigate to some other cell(s) outside this range.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question