范围内每个单元格的不同颜色

Different Color for Each Cell in a Range

我需要一些帮助, 我需要我的宏来为范围内的每个单元格着色,但每个单元格的颜色必须与上面的单元格不同。我当前使用的代码不执行该区分。代码是:

Function intRndColor()
    'USE - FUNCTION TO PICK RANDOM COLOR, ALSO ALLOWS EXCLUSION OF COLORS YOU DON'T LIKE
    Dim Again As Label
    Dim RangeX As Range
    Set RangeX = Range(Range("A1"), Range("A1").End(xlDown))

    Again:
        intRndColor = Int((50 * Rnd) + 1) 'GENERATE RANDOM IN

        Select Case intRndColor
            Case Is = 0, 1, 5, 9, 3, 13, 29, 30, 11, 21, 25, 29, 30, 32, 49, 51, 52, 55, 56 'COLORS YOU DON'T WANT
                GoTo Again
            Case Is = pubPrevColor
                GoTo Again
        End Select

        pubPrevColor = intRndColor 'ASSIGN CURRENT COLOR TO PREV COLOR

        ' Range(Range("A1"), Range("A1").End(xlDown)).Interior.ColorIndex = pubPrevColor

        For Each c In RangeX
            c.Interior.ColorIndex = pubPrevColor
        Next c
End Function

此代码使整个范围成为相同的颜色,我不明白我在这里遗漏了什么...

我认为你的循环搞混了。循环(使用 goto/label 创建)应该在循环内通过范围内的每个单元格:

Function intRndColor()
    'USE - FUNCTION TO PICK RANDOM COLOR, ALSO ALLOWS EXCLUSION OF COLORS YOU DON'T LIKE

    Dim c as Range
    Dim RangeX As Range
    Set RangeX = Range(Range("A1"), Range("A1").End(xlDown))

    'Loop through each cell in range
    For Each c In RangeX

        'Bounce back to this label if the random color is a color we don't want, or the previous color
        Again:
            intRndColor = Int((50 * Rnd) + 1) 'GENERATE RANDOM COLOR INT
            Select Case intRndColor
                Case Is = 0, 1, 5, 9, 3, 13, 29, 30, 11, 21, 25, 29, 30, 32, 49, 51, 52, 55, 56 'COLORS YOU DON'T WANT
                    GoTo Again
                Case Is = pubPrevColor
                    GoTo Again
            End Select

        'Paint the cell we are on
        c.Interior.ColorIndex = intRndColor

        'Set pubPrevColor
        pubPrevColor = intRndColor
    Next c
End Function

您正确地选择了一种随机颜色(尽管最大值为 51)。然后,您只需将一种颜色应用于所有单元格。每次将其应用于单元格时,您需要选择一种随机颜色。

如果你想不使用 GoTo

Dim RangeX As Range, avoidcolours As String, intRndColor As Long, firstcell As Boolean
avoidcolours = ",0,1,5,9,3,13,29,30,11,21,25,29,30,32,49,51,52,55,56,"

Set RangeX = Range(Range("A1"), Range("A1").End(xlDown))
firstcell = True

'Cycle through cells
For Each c In RangeX.Cells
    If firstcell Then
        'Pick random starting colour
        intRndColor = 0
        Do Until InStr(1, avoidcolours, "," & intRndColor & ",") = 0
            intRndColor = Int((50 * Rnd) + 1)
        Loop
        firstcell = False
    Else
        'Pick random colour
        Do Until intRndColor <> c.Offset(-1, 0).Interior.ColorIndex And InStr(1, avoidcolours, "," & intRndColor & ",") = 0
            intRndColor = Int((55 * Rnd) + 1)
        Loop
    End If
    c.Interior.ColorIndex = intRndColor
Next c

一个稍微简洁的方法是创建一个循环来应用随机颜色和一个函数来生成数字:

Sub applycolours()
    'USE - APPLYS RANDOM COLOURS TO CELLS, DIFFERING FROM CELL ABOVE
    Dim RangeX As Range, intRndColor As Long, firstcell As Boolean

    Set RangeX = Range(Range("A1"), Range("A1").End(xlDown))
    firstcell = True
    'Cycle through cells
    For Each c In RangeX.Cells
        If firstcell Then
            'Pick random starting colour
            intRndColor = randomcolour
            firstcell = False
        Else
            'Pick random colour
            Do Until intRndColor <> c.Offset(-1, 0).Interior.ColorIndex
                intRndColor = randomcolour
            Loop
        End If
        c.Interior.ColorIndex = intRndColor
    Next c
End Sub

Function randomcolour() as long
    'USE - FUNCTION TO PICK RANDOM COLOR, ALSO ALLOWS EXCLUSION OF COLORS YOU DON'T LIKE
    Dim avoidcolours as String
    avoidcolours = ",0,1,5,9,3,13,29,30,11,21,25,29,30,32,49,51,52,55,56,"
    randomcolour = 0
    Do Until InStr(1, avoidcolours, "," & randomcolour & ",") = 0
        randomcolour = Int((55 * Rnd) + 1)
    Loop
End Function