Hello There,
I am using validations on certain pre-defined cell Ranges.
If the User enters a wrong value then it should to the range --> Sub IncorrectField()
If the User enters a correct value, then 1st it should check if it has the above highlight. If yes then apply Sub CorrectField()
I have recorded this in a macro as listed below Subs
I am using the OnKey functionality, to navigate to various fields using the Named Range. I would like to apply these highlight validation subs from that module. Is there a smarter way to shorten the code and also if I can use the name range as a parameter instead?
For example the below code runs for a Named Range "CurrencyCode". Attached snapshot for your reference.
Many thanks for your help.
Regards,
Don
Sub IncorrectField
Sub CorrectField
I am using validations on certain pre-defined cell Ranges.
If the User enters a wrong value then it should to the range --> Sub IncorrectField()
If the User enters a correct value, then 1st it should check if it has the above highlight. If yes then apply Sub CorrectField()
I have recorded this in a macro as listed below Subs
I am using the OnKey functionality, to navigate to various fields using the Named Range. I would like to apply these highlight validation subs from that module. Is there a smarter way to shorten the code and also if I can use the name range as a parameter instead?
For example the below code runs for a Named Range "CurrencyCode". Attached snapshot for your reference.
Many thanks for your help.
Regards,
Don
Sub IncorrectField
Code:
Sub IncorrectField()
'------------------------------------------------------------------------
'This is to make the current cell fill yellow and red bottom border
'------------------------------------------------------------------------
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -11579393
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 9699327
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub
Sub CorrectField
Code:
Sub CorrectField()
'------------------------------------------------------------------------
'This is to make the current cell normal again
'------------------------------------------------------------------------
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Color = -5592402
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = -5592402
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -5592402
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Color = -5592402
.TintAndShade = 0
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End Sub