Results 1 to 7 of 7

Thread: If cell is colored enter a letter to it

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    50
    Location

    If cell is colored enter a letter to it

    I want to look at a column and if a cell has been highlighted by a certain color then place a letter N in that cell. Also if a different color is in the column then place a LS in that cell.
    I am not sure what I am missing. It looks Ok in the module and does not flag until I try to run it.

    If Range("F1:F101").Cell.Interior.ColorIndex = 4 Then
        Cell.Value = "H"
        If Cell.Interior.ColorIndex = 15 Then
            Cell.Value = "LS"
        End IF
    Last edited by Aussiebear; 01-13-2025 at 12:30 AM. Reason: Added code tags

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    Are these cell colors set with Conditional Formatting? Possibly VBA cannot read color codes set by CF. Seems I read that somewhere.

    I will have to do some testing.

    Your code structure is not valid.

    Confirmed, won't work with CF color setting.

    Otherwise, consider:
    Sub ChangeValueBasedOnCellColor()
        Dim rg As Range, xRg As Range
        Set xRg = Worksheets("Sheet1").Range("F1:F101")
        Application.DisplayAlerts = False
        For Each rg In xRg
            With rg
                Select Case .Interior.ColorIndex
                    Case Is = 4
                        .Value = "H"
                    Case Is = 15
                        .Value = "LS"
                End Select
            End With
        Next
        Application.DisplayAlerts = True
    End Sub
    Last edited by June7; 01-12-2025 at 10:24 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,496
    Location
    You might use this to determine the colour of a CF cell, but its currently only as a Message box output.

    Sub CheckConditionalFormattingColor()
        Dim cell As Range
        Set cell = Range("A1") 
        ' Replace with the cell you want to check
        If cell.DisplayFormat.Interior.ColorIndex <> xlNone Then 
            ' Cell has a color applied (either directly or by Conditional Formatting)
            MsgBox "Cell " & cell.Address & " has color index: " & cell.DisplayFormat.Interior.ColorIndex
        Else
            MsgBox "Cell " & cell.Address & " has no color."
        End If
    End Sub
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    Adding DisplayFormat to code DOES detect CF colors.
    Sub ChangeValueBasedOnCellColor()    
        Dim rg As Range, xRg As Range
        Set xRg = Worksheets("Sheet1").Range("F1:F101")
        Application.DisplayAlerts = False
        For Each rg In xRg
            With rg
                Select Case .DisplayFormat.Interior.ColorIndex
                    Case Is = 4
                        .Value = "H"
                    Case Is = 15
                        .Value = "LS"
                End Select
            End With
        Next
        Application.DisplayAlerts = True 
    End Sub
    Last edited by June7; 01-13-2025 at 11:53 AM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    VBAX Regular
    Joined
    Jul 2008
    Posts
    50
    Location
    Hi June7
    This works perfectly
    Thank you very much.
    I see where I went wrong and now I am just stepping through your example so I have a better understanding
    Kisinana

  6. #6
    there are a few issues with your current approach:

    1. Range("F1:F101").Cell is not correct. To loop through each cell in a range, you need to use a For Each loop.
    2. The logic inside your If statements needs to be adjusted so that you're checking the color for each individual cell.
    3. The Cell.Value = "LS" should be inside the second If statement, but it should only be executed when the color of the cell is 15.

  7. #7
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,496
    Location
    @adhochicken. I have removed your spam link in your signature. I would advise you not to spam again.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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