Excel中行的交替着色组

我有这样的Excel电子表格

 id |  id的数据
    | 更多id的数据
 id |  id的数据
 id |  id的数据
    | 更多id的数据
    | 甚至更多的数据为id
 id |  id的数据
    | 更多id的数据
 id |  id的数据
 id |  id的数据
    | 更多id的数据

现在我想通过交替行的背景颜色来组合一个id的数据

 var color = white
为每一行
    如果第一个单元格不是空的,颜色是白色的
        将颜色设置为绿色
    如果第一个单元格不是空的,颜色是绿色的
        将颜色设置为白色
    设置行的背景颜色

任何人都可以帮我一个macros或一些VBA代码

谢谢

我认为这是你正在寻找的。 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 

我使用这个公式来获得条件格式的input:

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

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

在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

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

在这里输入图像说明

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

根据Jason Z的回答,从我的testing中看来,这似乎是错误的(至less在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 

我正在挖掘这个,并试图修改它以供我使用。 我在第一列有订单号,一些订单有多行。 只是想要replace白色和灰色的每个订单号码。 我在这里交替每行。

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

结束小组

如果select“格式”菜单项下的“条件格式”菜单选项,则会给出一个对话框,让您构build一些适用于该单元格的逻辑。

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

单元格值是| 等于| | 和| 白色….然后select颜色。

您可以select添加button,并使条件尽可能大。

我根据可configuration的列,使用RGB值重新编制了Bartdude的答案,浅灰/白色。 当值改变时,布尔variables被翻转,并且这个variables用来通过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 

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

  1. 突出显示您想要应用交替样式的行。
  2. 按“条件格式” – >“新build规则”
  3. select“使用公式确定要格式化的单元格”(最后一项)
  4. 以格式值input规则: =MOD(ROW(),2)=0
  5. 按“格式”,对交替行进行必要的格式化,例如。 填充 – >颜色。
  6. 按OK,按OK。

如果您希望格式化交替列,请使用=MOD(COLUMN(),2)=0

瞧!