We help IT Professionals succeed at work.

Forcing multiple data validations?

Ryan Rood
Ryan Rood asked
on
Here is my issue. In Cell G29 G30 G31 G32 G33 I have a list of values in a hidden sheet that they can key in a specific value from the list (this works). It uses data validation to confirm the value entered is in the list.

What I need to do now is add a second validation that checks to see if there is a value <> 0 in H29 H30 H31 H32 H33. If there is a value in H then check to make sure there is something keyed into G.

It doesn't matter how this is accomplished as long as it works.

Any thoughts? Is VB script in the sheet the best route? Is there a simple way I am not thinking about? Keeping in mind that I have no coding experience.

Thanks,
Ryan
Comment
Watch Question

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Add this to the worksheet and let me know if it does what you want.

Option Explicit
Private mbSelectedViaCode As Boolean
Private mrngOldCell As Range
Private Sub Worksheet_Change(ByVal Target As Range)
' When G is changed and H is blank then set the focus to H
If Not Intersect(Target, Range("G29:G33")) Is Nothing Then
    If Target.Value <> "" And Target.Offset(0, 1).Value = "" Then
        Application.EnableEvents = False
        mbSelectedViaCode = True
        Target.Offset(0, 1).Select
        Set mrngOldCell = ActiveCell
        Application.EnableEvents = True
    End If
End If

'If we are in H and G has a value, H can't be blank
If Not Intersect(Target, Range("H29:H33")) Is Nothing Then
    If Target.Value = "" And Target.Offset(0, -1).Value <> "" Then
        MsgBox "There must be a value in this cell if cell " & StripDS(Target.Offset(0, -1).Address) _
             & " is not blank. Changing " & StripDS(Target.Address) & " to " & Target.Offset(0, -1).Value & "."
        Application.EnableEvents = False
        Target.Value = Target.Offset(0, -1).Value
        Application.EnableEvents = True
    End If
End If

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If mbSelectedViaCode Then
    mbSelectedViaCode = False
    Exit Sub
End If
'If we were in H and G has a value, H can't be blank
If Not mrngOldCell Is Nothing Then
    If Not Intersect(mrngOldCell, Range("H29:H33")) Is Nothing Then
       If mrngOldCell.Value = "" And mrngOldCell.Offset(0, -1).Value <> "" Then
            MsgBox "There must be a value in this cell if cell " & StripDS(mrngOldCell.Offset(0, -1).Address) _
                 & " is not blank. Changing " & StripDS(mrngOldCell.Address) & " to " & mrngOldCell.Offset(0, -1).Value & "."
            Application.EnableEvents = False
            mrngOldCell.Value = mrngOldCell.Offset(0, -1).Value
            Application.EnableEvents = True
        End If
    End If
End If

End Sub
Private Function StripDS(strAddress As String)
    ' Remove the dollar signs from an address
    StripDS = Replace(strAddress, "$", "")
End Function

Open in new window

Commented:
An alternative is to implement conditional formatting such that when G is blank and H<>0 then G is highlighted red. Does that sound suitable, or do you need active interference?

Author

Commented:
@Martin Liss - thank you - I have not had a chance to attempt this yet ... will give it a go this afternoon.

@scsyme - yes and no, I am already doing this with my current solution but wanted something a little more vibrant to grab their attention.

Author

Commented:
@Martin Liss: When I add the code to my sheet nothing happens. Can you post a sample workbook with your code in it working so I can compare how you are setting your document up to mine?

Thanks,
Ryan
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
It may or may not have been affecting your results, but I removed one variable in the attached workbook. The cells with data validation are blue.
Q-28627688.xlsm

Author

Commented:
I am trying to use your workbook as posted ... but I don't see anything happening? When data is typed in H29:H33 (any one), I want something to happen if there is nothing in G29:G33. Anything. An error message or something of the sorts that will pop up and warn them that they have not completed the field with data.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
OK, I misunderstood the requirements. In thought that if G29:G33 was changed that there needed to be something in H29:H33. Change a G29:G33 value. Do I have it backwards?

Author

Commented:
Yeah I think we are just off a little bit ... perhaps a bad explanation since only I know what I want. ;-)

Lets try this again:
If value is keyed into H29:H33 there must be a value in the corresponding G29:G33 range. If no value in G29:G33 pop an error warning them that they are missing a value in G29:G33.

Hopefully this is a little more clear.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Please run the workbook and change a G29:G33 cell. Other than the fact that it's backward is it what you want?

Author

Commented:
Yes - this is exactly what I am looking for. Are you able to reverse it?
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Yes. Be back soon.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Try this.
Q-28627688a.xlsm

Author

Commented:
This is what I am looking for ... one small thing ... can you disable adding the first value of the validation list? I just want them to get the error and the focus to move to this cell.
"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:

Author

Commented:
Thank you very much for your assistance! This is exactly what I was trying to accomplish.
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014