mercredi 27 juin 2018

How to assign random interior color of cells as I add names to a list?

This is my first time posting so please have mercy! I have tried to assign random colors to a name list so I can later recall and populate another list of a staffing sheet.

This is my code so far but it doesn't work for some odd reason. I'm not sure if someone already asked this question but my search has turned up empty handed.

Thanks!

Private Sub Worksheet_Change(ByVal Target As Range)
    Set WF = Application.WorksheetFunction
    If Target.Cells.Column = 1 Then
        If Target.Column = 3 Then
            x = 0
            On Error Resume Next
            x = WF.Match(Target.Value, _
                Range("C1").Resize(Target.Row - 1), _
                0)
            On Error GoTo 0
            If x > 0 Then
                ' duplicate value...copy the old color
                Target.Interior.Color = Cells(x, 3).Interior.Color
            Else
                ' choose a new color
                Target.Interior.Color = RGB( _
                    WF.RandBetween(0, 255), _
                    WF.RandBetween(0, 255), _
                    WF.RandBetween(0, 255))
            End If
        End If
    End If
End Sub




Aucun commentaire:

Enregistrer un commentaire