Solved

How do I create a mandatory field based on a condition

Posted on 2007-12-03
4
2,382 Views
Last Modified: 2010-04-21
In Microsoft Excel I would like to make a mandatory field "A2" based on the condition of another field "A1". For instance when I select the value 'yes' from a drop down in A1, I want to make the next field "A2" mandatory and it must be a date format. I've found the following code, but I don't know how to modify to the above process.

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

Any help is greatly appreciated
0
Comment
Question by:Boombox69
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 250 total points
ID: 20396153
Something like this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Range("A1").Value = "yes" and Range("A2").Value = "" Then
   on error resume next
   Application.EnableEvents = False
    Range("A2").Select
   Application.enableevents = true
   MsgBox ("You must enter a value for Cell A2")
 End If
End Sub

Regards,
Rory
0
 
LVL 38

Assisted Solution

by:jeverist
jeverist earned 250 total points
ID: 20396167
Hi Boombox69,

Try this:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim c1 As Range, c2 As Range

Application.EnableEvents = False

Set c1 = Me.Range("A1")
Set c2 = Me.Range("A2")

If Intersect(Target, c2) Is Nothing Then
    If LCase(c1.Value) = "yes" And IsEmpty(c2) Then
      MsgBox "You must enter a value for Cell: " & Replace(c2.Address, "$", "")
      Application.Goto c2
    End If
End If

Application.EnableEvents = True

End Sub

Jim
0
 

Author Closing Comment

by:Boombox69
ID: 31412341
You guy's are awesome. I'm sorry I had to split the points.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 20399808
That's exactly what you should do - thanks for the points. :)
Rory
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I need to access elements from an existing child window to extrat info! 19 45
Hard coding time and date into Excel 2 32
Boolean help 6 27
In search of x17-22375.exe 2 14
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now