【问题标题】:Different Color for Each Cell in a Range范围内每个单元格的不同颜色
【发布时间】:2020-02-12 14:46:11
【问题描述】:

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

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

这段代码使整个范围都是相同的颜色,我不明白我在这里缺少什么......

【问题讨论】:

  • 看看你的最终循环:For Each c In RangeXc.Interior.ColorIndex = pubPrevColorNext c...pubPrevColor永远不会改变。
  • @BigBen .Color 使用 RGB.ColorIndex 使用托盘。

标签: excel vba text-coloring


【解决方案1】:

您正确地选择了一种随机颜色(尽管最大为 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

【讨论】:

    【解决方案2】:

    我认为您的循环混淆了。循环(使用goto/标签创建)应该在您的循环内,通过范围内的每个单元格:

    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
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2013-11-15
      • 2015-11-16
      • 1970-01-01
      • 2017-01-05
      相关资源
      最近更新 更多