开发者

How to change default colors used in VBA code/Macro result (Red, Green)

I am using the following VBA code to change the color of the rows in my spreadsheet every time the value in Column A changes (So that all entries with the same value in column A will be grouped by color. The spreadsheet is sorted by column A already so the items are already grouped, I just needed them colored).

Anyway, when I run this macro the rows are colored red & green (which are very bright and overwhelming colors for this purpose). I need something more subtle..

How do I change this? Or can I specify in my VBA code for it to use certain colors by rgb or color index? {I am using 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).Valu开发者_运维百科e) 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 


Run this program (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

Output sample:

How to change default colors used in VBA code/Macro result (Red, Green)


You can customize the colors palette by code, I think the page here will answer your question: 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


It turns out all I had to do is change a few numbers in the code i posted in my question. I bolded the numbers I had to change. These numbers correspond to the color ID (like what Belisarious put). NOTE: I had to put apostrohpes so that the VBA code wouldn't be recognized as VBA code (because if it is it won't bold the numbers). See the original question for the correct code.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜