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

问题描述

我正在使用以下VBA代码,以便每次列A中的值更改电子表格中的行颜色(以便列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所说)。注意:我必须使用apostrohpes,以便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.

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

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)然后

退出

如果

'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 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代码/宏结果中使用的差异颜色(红色,绿色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 20:35