如何更改在VBA代码/macros结果(红色,绿色)中使用的颜色

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

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

我如何改变这个? 或者我可以在我的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 

运行这个程序(学分在这里)

 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 

输出样本:

替代文字

你可以通过代码自定义颜色调色板,我想这里的页面将回答你的问题: 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 

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

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

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

'对于r = 1到ActiveSheet.Rows.Count
如果IsEmpty(ActiveSheet.Cells(r,1).Value)那么
退出
万一

'如果ActiveSheet.Cells(r,1).Value <> val然后
如果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 

下一个

结束小组