Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Excel - Mandatory Cell

Status
Not open for further replies.

slickp007

MIS
Feb 18, 2008
46
GB
Excel 2003

What i'd like to do is, that if say cell A4 had any sort of text, number or date in it, then this then makes cell B4 mandatory. I've created some VBA that makes cell A4 mandatory:

Option Explicit
Dim Mandatory As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub

Select Case Target.Address
Case "$A$4"
Set Mandatory = Target
Case Else
If Not Mandatory Is Nothing Then
If Mandatory = "" Then
Mandatory.Select
MsgBox "You cannot leave this cell blank"
End If
End If
End Select


End Sub


However i need some help with how to make it so that if there is anything in A4 then B4 is mandatory. Help?
 
Please repost this VBA-specific question in forum707.

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 




He did post this in Forum707 thread707-1481177.

But I did point out that Data > Validation might be a better way to go.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
That makes sense, Skip. I didn't really look at what was being requested, just that VBA code was being sought.

Well, slickp007, did you try your hand at Data > Validation? Are you having problems with it?

When it comes time to validate the second cell dependent on the first, have a look here:

[tt]_____
[blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 
I'm having real problems with data validation for this. How can i force a cell not to be null using data validation?
 




I believe that I was the one who directed your toward Data > Validation.

It seems that ONLY when the cell is in the EDIT mode, will this kind of validation work to prevent no value from being entered. Data > Validation cannot require that a value be entered in a cell.

Another option:

UNLOCK all data entry cells.

Protect the sheet. This will enable tabbing from one data entry cell to the next.

This is the code that you will need.
[tt]
Insert a MODULE and declare a Public Variable
Code:
Public sPrev as string
Activate the ThisWorkbook Object Code Window and paste this code
Code:
Private Sub Workbook_Open()
    Sheets("[b]YourSheetName[/b]").Activate
    sPrev = ActiveCell.Address
End Sub
Activate the Sheet Object Code WIndow for the sheet with the data and paste this code
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Range(sPrev).Value = "" Then
        Application.EnableEvents = False
        Range(sPrev).Select
        Application.EnableEvents = True
    Else
        sPrev = Target.Address
    End If
End Sub
[/tt]
Add whatever message you wish.

Sorry for the miss-q.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top