• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Macro code to highlight cell -> How to convert into range & shorten the macro code?

inddon

Member
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
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
 

Attachments

  • Validation Highlight Colors.JPG
    Validation Highlight Colors.JPG
    35.7 KB · Views: 8
Back
Top