Results 1 to 14 of 14

Thread: Colour cell on click event

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,406
    Location

    Colour cell on click event

    This has been an extremely frustrating issue. In the attached workbook, I am looking for particular cells to either be coloured upon being clicked or un-coloured upon a second click.

    The code provided seemingly does nothing, even after Saving, Closing, then reopening the file. These are the versions tried, and before anyone asks, all lines were commented out before trying the newest version.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim ws As Worksheet
        Dim targetCell As Range
        Set ws = ThisWorkbook.Sheets("Sheet1")
        If Not Intersect(Target, ws.Range("B4:N4")) Is Nothing Then
            Set targetCell = Target
            If targetCell.Interior.Color = ws.Range("B4").Interior.Color Then
                targetCell.Interior.Color = xlNone
            Else
                targetCell.Interior.Color = ws.Range("B4").Interior.Color
            End If
        End If
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim targetCell As Range
        Dim orangeColor As Long
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1")
        orangeColor = RGB(255, 165, 0) ' Explicit orange color
        If Not Intersect(Target, ws.Range("B4:N4")) Is Nothing Then
            Set targetCell = Target
            If targetCell.Interior.Color = orangeColor Then
                targetCell.Interior.Color = xlNone
            Else
                targetCell.Interior.Color = orangeColor
            End If
        End If
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim targetCell As Range
        Dim orangeColor As Long
        Dim ws As Worksheet
        Dim targetRange As Range ' Explicitly declare Target
        Set ws = ThisWorkbook.Sheets("Sheet1")
        orangeColor = RGB(255, 165, 0)
        Set targetRange = Target ' Set targetRange
        If Not Intersect(targetRange, ws.Range("B4:N4")) Is Nothing Then
            Set targetCell = targetRange
            If targetCell.Interior.Color = orangeColor Then
                targetCell.Interior.Color = xlNone
            Else
                targetCell.Interior.Color = orangeColor
            End If
        End If
    End Sub
    
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Call ToggleCellColor
    End Sub
    The ultimate aim is to allow the User to select the preferred months of the year for his/her area for each of the sectors in Range A4 to A9 as the criteria.

    Colours used with colour Index #
    White, Background 1, 15% darker (16)
    Orange (48)
    Plum, Accent 5 (56)
    Turquoise, Accent 4 Lighter 40% (10)
    Red (3)

    I've had that mmany swings and misses, I can't tell if i've been struck out or thrown out.
    Attached Files Attached Files
    Last edited by Aussiebear; 03-20-2025 at 02:00 PM.
    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
  •