本文介绍了如何更改VBA代码/宏结果中使用的默认颜色(红色,绿色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次使用A列中的值更改时,我都会使用以下VBA代码更改电子表格中行的颜色(以便将A列中具有相同值的所有条目都按颜色分组.电子表格已排序按列A排列,因此项目已经分组,我只需要对它们上色即可).

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..

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

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).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 

推荐答案

原来,我要做的就是更改我在问题中发布的代码中的一些数字.我修改了我必须更改的数字.这些数字对应于颜色ID(例如Belisarious的名称).注意:我必须加撇号,以使VBA代码不会被识别为VBA代码(因为如果这样,它将不会加粗数字).请参阅原始问题以获取正确的代码.

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.

昏暗r一样长,val一样长,c一样长

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

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

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

'对于r = 1到ActiveSheet.Rows.Count
如果IsEmpty(ActiveSheet.Cells(r,1).Value)然后
退出
如果结束

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

'如果ActiveSheet.Cells(r,1).Value<> val然后
如果c = 3 然后
c = 4
其他
c = 3
如果结束,
如果结束

' 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  

下一步

结束字幕

这篇关于如何更改VBA代码/宏结果中使用的默认颜色(红色,绿色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 00:44