Consulting

Results 1 to 16 of 16

Thread: Worksheet Function Change Row Colors According to Value

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location

    Worksheet Function Change Row Colors According to Value

    I have done a service schedule in Excel, What i am trying to get it to do is when you put an "x" in a cell it will automatically change the color of the cell.
    Below is the code i have written, Can someone throw some light on what i am doing wrong(probaly everything)
    Thanks
    Ian


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim x As Integer
    Dim s As Integer
    Dim m As Integer
      With Range("A1:z150")
        If Cells.Value = x Then
            Cells.Interior.ColorIndex = 34
            If Cells.Value = s Then
                Cells.Interior.ColorIndex = 32
            End If
        End If
    End With
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 04:19 PM. Reason: Adjusted the code tags

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    hello Ian!

    what are x, s, and m values? or do you mean the characters "x", "s" & "m"?

    and will there be more than only those?

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    thanks for the reply Zack
    yes they are the characters, there will be probabley 5 characters altogether, ie: m will stand for mot, s for service and so on.

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    hello Ian, sorry about the delay, got busy suddenly. try this in your worksheet code...

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 26 Or Target.Row > 150 Then Exit Sub
        On Error GoTo err1
        Select Case Target.Value
            Case Is = "x"
                Target.Interior.ColorIndex = 34
            Case Is = "s"
                Target.Interior.ColorIndex = 32
            Case Is = "m"
                Target.Interior.ColorIndex = 55
            Case Is = ""
                Target.Interior.ColorIndex = 0
                err1: Exit Sub
        End Select
    End Sub

    substitute for your desired changes, and add as needed.

    does this help?
    Last edited by Aussiebear; 04-27-2023 at 04:20 PM. Reason: Adjusted the code tags

  5. #5
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thanks Zack

    Just what i needed

    Ian

  6. #6
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, guys!!

    Ian: Welcome to the board!

    Is this some code we should push to the knowledgebase?
    Is it something others might be able to use sometime, and can we have your permission to post it up there, Ian?

    What do you think?

    Please notice the new ranking icons for people with knowledgebase contributions.
    ~Anne Troy

  7. #7
    VBAX Tutor SJ McAbney's Avatar
    Joined
    May 2004
    Location
    Glasgow
    Posts
    243
    Location
    Is there somewhere that lists what all the different colours and/or quantities mean with respect to contributions?

  8. #8
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Hi Anne

    Yes i think it could be useful to others
    Here is a bit of code i found that lists all the colors with their respective numbers


    Sub colors56()
    '57 colors, 0 to 56
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual 'pre XL97 xlManual
    Dim i As Long
    Dim str0 As String, str As String
    For i = 0 To 56
        Cells(i + 1, 1).Interior.colorindex = I
        Cells(i + 1, 1).Value = "[Color " & i & "]"
        Cells(i + 1, 2).Font.colorindex = I
        Cells(i + 1, 2).Value = "[Color " & i & "]"
        str0 = Right("000000" & Hex(Cells(i + 1, 1).Interior.color), 6)
        'Excel shows nibbles in reverse order so make it as RGB
        str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
        'generating 2 columns in the HTML table
        Cells(i + 1, 3) = "#" & str & "#" & str & ""
        Cells(i + 1, 4).Formula = "=Hex2dec(""" & Right(str0, 2) & """)"
        Cells(i + 1, 5).Formula = "=Hex2dec(""" & Mid(str0, 3, 2) & """)"
        Cells(i + 1, 6).Formula = "=Hex2dec(""" & Left(str0, 2) & """)"
        Cells(i + 1, 7) = "[Color " & i & ")"
    Next i
    done:
    Application.Calculation = xlCalculationAutomatic 'pre XL97 xlAutomatic
    Application.ScreenUpdating = True
    End Sub


    Regards
    Ian
    Last edited by Aussiebear; 04-27-2023 at 04:21 PM. Reason: Adjusted the code tags

  9. #9
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, Abulafia: Not yet. I need to create a page.

    Ian: Thanks! Can you make a sample file with that in it?
    ~Anne Troy

  10. #10
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Sorry it took so long to get back to you I just got side tracked on something else, But here is my demo file showing everything is working as it should

    Regards

    Ian

  11. #11
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Not a problem, Ian!

    I'm waiting for our submission form anyway, now.
    No sense in building manually what can be automated, right?

    ~Anne Troy

  12. #12
    VBAX Contributor Ivan F Moala's Avatar
    Joined
    May 2004
    Location
    Auckland New Zealand
    Posts
    185
    Location
    Quote Originally Posted by rama4672
    Hi Anne

    Yes i think it could be useful to others
    Here is a bit of code i found that lists all the colors with their respective numbers


    Sub colors56()
    '57 colors, 0 to 56
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual 'pre XL97 xlManual
    Dim i As Long
    Dim str0 As String, str As String
    For i = 0 To 56
        Cells(i + 1, 1).Interior.colorindex = I
        Cells(i + 1, 1).Value = "[Color " & i & "]"
        Cells(i + 1, 2).Font.colorindex = I
        Cells(i + 1, 2).Value = "[Color " & i & "]"
        str0 = Right("000000" & Hex(Cells(i + 1, 1).Interior.color), 6)
        'Excel shows nibbles in reverse order so make it as RGB
        str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
        'generating 2 columns in the HTML table
        Cells(i + 1, 3) = "#" & str & "#" & str & ""
        Cells(i + 1, 4).Formula = "=Hex2dec(""" & Right(str0, 2) & """)"
        Cells(i + 1, 5).Formula = "=Hex2dec(""" & Mid(str0, 3, 2) & """)"
        Cells(i + 1, 6).Formula = "=Hex2dec(""" & Left(str0, 2) & """)"
        Cells(i + 1, 7) = "[Color " & i & ")"
    Next i
    done:
    Application.Calculation = xlCalculationAutomatic 'pre XL97 xlAutomatic
    Application.ScreenUpdating = True
    End Sub
    Regards
    Ian
    Ian just a couple of things
    1) that code requires the AnalysisTool pac VBA installed in order to be run.
    2) There are only 56 colors your indexing starts @ 0 - 56 = 57
    3) The Hex values don't look right? or rather they have doubled

    here is an alternative with no ATP required.

    Sub GetRGB_ColorIndex()
    '// Ivan F Moala
    '// http://www.xcelfiles.com
    Dim k, x As Single
    '// 1st Clear Old data
    Range([A1:G1], [A1:G1].End(xlDown)).ClearContents
    x = 1
    Frmt
    Application.ScreenUpdating = False
    [A1] = "ColorIndex"
    [B1] = "Color"
    [C1] = "LongValue"
    [D1] = "RED"
    [E1] = "GREEN"
    [F1] = "BLUE"
    [G1] = "HEX"
    For Each k In ActiveWorkbook.Colors
        Cells(x + 1, 1) = x 'ColorIndex
        Cells(x + 1, 2).Interior.ColorIndex = x 'Color
        Cells(x + 1, 3) = k 'Longvalue
        Cells(x + 1, 4) = Cells(x + 1, 3).Value And &HFF 'RED
        Cells(x + 1, 5) = (Cells(x + 1, 3).Value And &H100FF00) / &H100 'GREEN
        Cells(x + 1, 6) = (Cells(x + 1, 3).Value And &HFF0000) / &H10000 'BLUE
        Cells(x + 1, 7) = Hex((Cells(x + 1, 3).Value)) 'HEX
        x = x + 1
    Next k
    Columns("A:G").Columns.AutoFit
    Application.ScreenUpdating = True
    End Sub
    
    Sub Frmt()
    With Range("A1:H58")
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .Columns.AutoFit
        .AutoFormat Format:=xlRangeAutoFormat3DEffects2, Number:=False, _
        Font:=False, Alignment:=False, Border:=True, Pattern:=True, Width:= _
        False
    End With
    With Range("D1")
        .Interior.ColorIndex = 3
        .Font.ColorIndex = 2
    End With
    With Range("F1")
        .Interior.ColorIndex = 5
        .Font.ColorIndex = 2
    End With
    Range("E1").Interior.ColorIndex = 4
    Range("G1").Interior.ColorIndex = 36
    Range("A1:G1").Font.Bold = True
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 04:25 PM. Reason: Adjusted the code tags
    Kind Regards,
    Ivan F Moala From the City of Sails

  13. #13
    VBAX Regular
    Joined
    May 2004
    Location
    UK
    Posts
    71
    Location
    Thanks for that ivan
    Thats probably why it works on one machine which i have the tools pak installed but not on the other

    Regards

    Ian

  14. #14
    Hi, I was wondering if the following code can be altered a little so as to highlight the entire row, and not just the just the first cell in the column.


    Quote Originally Posted by firefytr
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column > 26 Or Target.Row > 150 Then Exit Sub
    On Error GoTo err1
    Select Case Target.Value
        Case Is = "x"
            Target.Interior.ColorIndex = 34
        Case Is = "s"
            Target.Interior.ColorIndex = 32
        Case Is = "m"
            Target.Interior.ColorIndex = 55
        Case Is = ""
           Target.Interior.ColorIndex = 0
        err1: Exit Sub
    End Select
    End Sub
    I was also wanted to use the 4th column as a my target column, rather than the first. Can anyone help me?

    Thanks
    Last edited by Aussiebear; 04-27-2023 at 04:28 PM. Reason: Adjusted the code tags

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by jacksonworld
    Hi, I was wondering if the following code can be altered a little so as to highlight the entire row, and not just the just the first cell in the column.

    I was also wanted to use the 4th column as a my target column, rather than the first. Can anyone help me?

    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ws_exit:
        Application.EnableEvents = False
        With Target
        If .Column = 4 Then
            Select Case .Value
                Case Is = "x"
                    .EntireRow.Interior.ColorIndex = 34
                Case Is = "s"
                    .EntireRow.Interior.ColorIndex = 32
                Case Is = "m"
                    .EntireRow.Interior.ColorIndex = 55
                Case Is = ""
                    .EntireRow.Interior.ColorIndex = 0
            End Select
        End If
        End With
    ws_exit:
        Application.EnableEvents = True
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 04:29 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16

    Thumbs up

    Perfect. Thanks for your help. Much appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •