【问题标题】:How to change default colors used in VBA code/Macro result (Red, Green)如何更改 VBA 代码/宏结果中使用的默认颜色(红色、绿色)
【发布时间】:2011-05-21 04:19:16
【问题描述】:

每次 A 列中的值发生变化时,我都使用以下 VBA 代码更改电子表格中行的颜色(以便 A 列中具有相同值的所有条目将按颜色分组。电子表格已排序已经按 A 列,所以项目已经分组,我只需要它们着色)。

无论如何,当我运行这个宏时,行是红色和绿色的(为此目的,这是非常明亮和压倒性的颜色)。我需要一些更微妙的东西..

我该如何更改?或者我可以在我的 VBA 代码中指定它通过 rgb 或颜色索引使用某些颜色吗? {我使用的是 Excel 2007}

Sub colorize() 

Dim r As Long, val As Long, c As Long 

r = 1 
val = ActiveSheet.Cells(r, 1).Value 
c = 4 

For r = 1 To ActiveSheet.Rows.Count 
    If IsEmpty(ActiveSheet.Cells(r, 1).Value) Then 
        Exit For 
    End If 

    If ActiveSheet.Cells(r, 1).Value <> val Then 
        If c = 3 Then 
             c = 4 
        Else 
            c = 3 
        End If 
    End If 

    ActiveSheet.Rows(r).Select 
    With Selection.Interior 
        .ColorIndex = c 
        .Pattern = xlSolid 
    End With 

    val = ActiveSheet.Cells(r, 1).Value 
Next 

End Sub 

【问题讨论】:

    标签: vba excel background-color


    【解决方案1】:

    运行这个程序(credits here)

    Sub colors56()
    '57 colors, 0 to 56
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual   'pre XL97 xlManual
    Dim i As Long
    Dim str0 As String, str As String
    For i = 0 To 56
      Cells(i + 1, 1).Interior.ColorIndex = i
      Cells(i + 1, 1).Value = "[Color " & i & "]"
      Cells(i + 1, 2).Font.ColorIndex = i
      Cells(i + 1, 2).Value = "[Color " & i & "]"
      str0 = Right("000000" & Hex(Cells(i + 1, 1).Interior.Color), 6)
      'Excel shows nibbles in reverse order so make it as RGB
      str = Right(str0, 2) & Mid(str0, 3, 2) & Left(str0, 2)
      'generating 2 columns in the HTML table
      Cells(i + 1, 3) = "#" & str & "#" & str & ""
      Cells(i + 1, 4).Formula = "=Hex2dec(""" & Right(str0, 2) & """)"
      Cells(i + 1, 5).Formula = "=Hex2dec(""" & Mid(str0, 3, 2) & """)"
      Cells(i + 1, 6).Formula = "=Hex2dec(""" & Left(str0, 2) & """)"
      Cells(i + 1, 7) = "[Color " & i & ")"
    Next i
    done:
      Application.Calculation = xlCalculationAutomatic  'pre XL97 xlAutomatic
      Application.ScreenUpdating = True
    End Sub
    

    输出样本:

    【讨论】:

    • @Colton 您的评论中缺少某些内容
    • 感谢 belisarius,我在完成该评论之前就已经弄清楚了,并不想离开它。但是现在我不能把它脱下来。
    【解决方案2】:

    您可以通过代码自定义调色板,我认为这里的页面会回答您的问题: http://www.databison.com/index.php/excel-color-palette-and-color-index-change-using-vba/

    Sub change_palette_color
        dim color_index as long
        color_index = 10
        ActiveWorkbook.Colors(color_index) = RGB(128, 128, 128)
    End sub
    

    【讨论】:

      【解决方案3】:

      事实证明,我所要做的就是更改我在问题中发布的代码中的一些数字。我加粗我必须更改的数字。这些数字对应于颜色 ID(就像 Belisarious 放的一样)。注意:我必须加上撇号,这样 VBA 代码就不会被识别为 VBA 代码(因为如果它不会加粗数字)。请参阅原始问题以获取正确的代码。

      Dim r As Long, val As Long, c As Long

      'r = 1
      'val = ActiveSheet.Cells(r, 1).Value
      'c = 4

      'For r = 1 To ActiveSheet.Rows.Count
      If IsEmpty(ActiveSheet.Cells(r, 1).Value) Then
      退出
      结束如果

      ' If ActiveSheet.Cells(r, 1).Value val Then
      如果 c = 3 那么
      c = 4
      其他
      c = 3
      结束如果
      结束如果

      ActiveSheet.Rows(r).Select  
      With Selection.Interior  
          .ColorIndex = c  
          .Pattern = xlSolid  
      End With  
      
      val = ActiveSheet.Cells(r, 1).Value  
      

      下一个

      结束子

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-08
        • 2015-09-29
        • 1970-01-01
        • 2021-07-19
        • 2012-01-26
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多