2008-08-25 171 views
10

我有一个Excel电子表格这样交替着色行组在Excel

 
id | data for id 
    | more data for id 
id | data for id 
id | data for id 
    | more data for id 
    | even more data for id 
id | data for id 
    | more data for id 
id | data for id 
id | data for id 
    | more data for id 

现在我要组一个ID的数据通过交替行的背景颜色

 
var color = white 
for each row 
    if the first cell is not empty and color is white 
     set color to green 
    if the first cell is not empty and color is green 
     set color to white 
    set background of row to color 

灿谁能帮我用宏或一些VBA代码

感谢

回答

4

我认为这是你正在寻找的。当A列中的单元格更改值时翻转颜色。运行,直到有在列B没有价值

Public Sub HighLightRows() 
    Dim i As Integer 
    i = 1 
    Dim c As Integer 
    c = 3  'red 

    Do While (Cells(i, 2) <> "") 
     If (Cells(i, 1) <> "") Then 'check for new ID 
      If c = 3 Then 
       c = 4 'green 
      Else 
       c = 3 'red 
      End If 
     End If 

     Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c 
     i = i + 1 
    Loop 
End Sub 
+0

我可能误解了,但从我看到它和从我的测试中,这切换颜色从一行到另一行,只要A列和B列不是空的......所以不回答问题。但我可能错过了一些... – 2013-06-06 12:07:08

0

如果选择条件在“格式”菜单项下的格式菜单选项中,您将看到一个对话框,让您构建一些适用于该单元格的逻辑。

你的逻辑可能不一样上面的代码,它可能看起来更像:

单元格的值|等于| |和|白色....然后选择颜色。

您可以选择添加按钮并使条件尽可能大。

1

你必须使用代码吗? 如果表格是静态的,那么为什么不使用自动格式化功能?

enter image description here

它也可以帮助您解决“合并单元格”相同的数据。因此,如果将“数据,更多数据,甚至更多数据”的单元格合并到一个单元格中,则可以更轻松地处理经典“每行都是一行”的情况。

+0

这是我需要但不知道如何做到这一点:)根据提供的解释 – 2014-10-28 17:21:40

33

我用这个公式来获得输入的条件格式:

=IF(B2=B1,E1,1-E1)) [content of cell E2] 

当B列包含需要进行分组和E项目是一个辅助列。每次上层单元格(此例中的B1)与当前单元格(B2)相同时,将返回列E中的上一行内容。否则,它将返回1减去该内容(也就是说,outupt将为0或1,具体取决于上面的单元格的值)。

enter image description here

enter image description here

enter image description here

+6

很好的答案。作为MOD功能的替代品,您可以使用1-E1。所以完整的公式是`= IF(B2 = B1,E1,1-E1)` – Kasaku 2012-03-20 17:46:28

+3

在我的情况下,我不能在公式中使用分号(;),Excel中只能使用逗号(,)。我正在使用MS Excel v 14.0.7106.5003 32位。 – rockXrock 2014-01-10 01:45:35

+1

是否可以避免使用额外的列? – 2014-09-03 19:24:47

1

基于杰森个Z回答,从我的测试似乎是错误的(至少在Excel 2010中),这里的一些代码恰好工作对我来说:

Public Sub HighLightRows() 
    Dim i As Integer 
    i = 2 'start at 2, cause there's nothing to compare the first row with 
    Dim c As Integer 
    c = 2  'Color 1. Check http://dmcritchie.mvps.org/excel/colors.htm for color indexes 

    Do While (Cells(i, 1) <> "") 
     If (Cells(i, 1) <> Cells(i - 1, 1)) Then 'check for different value in cell A (index=1) 
      If c = 2 Then 
       c = 34 'color 2 
      Else 
       c = 2 'color 1 
      End If 
     End If 

     Rows(Trim(Str(i)) + ":" + Trim(Str(i))).Interior.ColorIndex = c 
     i = i + 1 
    Loop 
End Sub 
1

我正在发生这种情况,并试图修改它以供我使用。我在a栏中有订单号码,有些订单需要多行。只需要交替白色和灰色每个订单号码。我在这里交替每行。

ChangeBackgroundColor() ' ChangeBackgroundColor Macro ' ' Keyboard Shortcut: Ctrl+Shift+B Dim a As Integer a = 1 Dim c As Integer c = 15 'gray Do While (Cells(a, 2) <> "") If (Cells(a, 1) <> "") Then 'check for new ID If c = 15 Then c = 2 'white Else c = 15 'gray End If End If Rows(Trim(Str(a)) + ":" + Trim(Str(a))).Interior.ColorIndex = c a = a + 1 Loop

End Sub

0

我已经返工Bartdude的回答,为浅灰色/基于配置的列白色的,使用RGB值。当值更改时,布尔变量将被翻转,并用于通过True和False的整数值对颜色数组进行索引。在2010年为我工作。用图纸编号调用子版。

Public Sub HighLightRows(intSheet As Integer) 
    Dim intRow As Integer: intRow = 2 ' start at 2, cause there's nothing to compare the first row with 
    Dim intCol As Integer: intCol = 1 ' define the column with changing values 
    Dim Colr1 As Boolean: Colr1 = True ' Will flip True/False; adding 2 gives 1 or 2 
    Dim lngColors(2 + True To 2 + False) As Long ' Indexes : 1 and 2 
      ' True = -1, array index 1. False = 0, array index 2. 
    lngColors(2 + False) = RGB(235, 235, 235) ' lngColors(2) = light grey 
    lngColors(2 + True) = RGB(255, 255, 255) ' lngColors(1) = white 

    Do While (Sheets(intSheet).Cells(intRow, 1) <> "") 
     'check for different value in intCol, flip the boolean if it's different 
     If (Sheets(intSheet).Cells(intRow, intCol) <> Sheets(intSheet).Cells(intRow - 1, intCol)) Then Colr1 = Not Colr1 
     Sheets(intSheet).Rows(intRow).Interior.Color = lngColors(2 + Colr1) ' one colour or the other 
     ' Optional : retain borders (these no longer show through when interior colour is changed) by specifically setting them 
     With Sheets(intSheet).Rows(intRow).Borders 
      .LineStyle = xlContinuous 
      .Weight = xlThin 
      .Color = RGB(220, 220, 220) 
     End With 
     intRow = intRow + 1 
    Loop 
End Sub 

可选奖励:为SQL数据,SSMS中使用的颜色的任何NULL值具有相同的黄色

Public Sub HighLightNULLs(intSheet As Integer) 
    Dim intRow As Integer: intRow = 2 ' start at 2 to avoid the headings 
    Dim intCol As Integer 
    Dim lngColor As Long: lngColor = RGB(255, 255, 225) ' pale yellow 

    For intRow = intRow To Sheets(intSheet).UsedRange.Rows.Count 
     For intCol = 1 To Sheets(intSheet).UsedRange.Columns.Count 
      If Sheets(intSheet).Cells(intRow, intCol) = "NULL" Then Sheets(intSheet).Cells(intRow, intCol).Interior.Color = lngColor 
     Next intCol 
    Next intRow 
End Sub 
0

我在Excel中使用这个规则来格式化交替行:

  1. 突出显示您想要应用交替样式的行。
  2. 按“条件格式” - >新建规则
  3. 选择“使用公式确定格式细胞”(最后一项)
  4. 格式值输入规则:=MOD(ROW(),2)=0
  5. 按“格式” ,为交替行进行必要的格式化,例如。填充 - >颜色。
  6. 按OK,按OK。

如果你想,而不是格式化交替列,使用=MOD(COLUMN(),2)=0

瞧!