Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel中行的交替着色组_Excel_Vba_Colors - Fatal编程技术网

Excel中行的交替着色组

Excel中行的交替着色组,excel,vba,colors,Excel,Vba,Colors,我有一个像这样的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 | id的数据 |id的

我有一个像这样的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 | id的数据 |id的更多数据 id | id的数据 id | id的数据 |id的更多数据 |id的更多数据 id | id的数据 |id的更多数据 id | id的数据 id | id的数据 |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代码吗


谢谢

如果您在“格式”菜单项下选择“条件格式”菜单选项,您将看到一个对话框,可以通过该对话框构造应用于该单元格的逻辑

您的逻辑可能与上面的代码不同,它可能更像:

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

您可以选择“添加”按钮,并根据需要将条件设置为最大。

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


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

我认为这正是您想要的。当列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
Public Sub-HighLightRows()
作为整数的Dim i
i=1
作为整数的Dim c
c=3'红色
Do While(单元格(i,2)”)
如果(单元格(i,1)”),则“检查新ID”
如果c=3,则
c=4'绿色
其他的
c=3'红色
如果结束
如果结束
行(修剪(Str(i))+“:”+修剪(Str(i))).Interior.ColorIndex=c
i=i+1
环
端接头

我使用此公式获取条件格式的输入:

=IF(B2=B1,E1,1-E1))    [content of cell E2]
其中,B列包含需要分组的项目,E列为辅助列。每次上层单元格(本例中为B1)与当前单元格(B2)相同时,都会返回E列的上层行内容。否则,它将返回1减去该内容(也就是说,outupt将是0或1,具体取决于上部单元格的值)


根据Jason 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
Public Sub-HighLightRows()
作为整数的Dim i
i=2'从2开始,因为没有任何东西可以与第一行进行比较
作为整数的Dim c
c=2'颜色1。检查http://dmcritchie.mvps.org/excel/colors.htm 颜色索引
Do While(单元格(i,1)”)
如果(单元格(i,1)单元格(i-1,1)),则检查单元格A中的不同值(索引=1)
如果c=2,则
c=34'颜色2
其他的
c=2'颜色1
如果结束
如果结束
行(修剪(Str(i))+“:”+修剪(Str(i))).Interior.ColorIndex=c
i=i+1
环
端接头

我正在推着它,并试图修改它以供我使用。我在a列有订单号,有些订单有多行。只是想在每个订单号上交替使用白色和灰色。我这里的东西每行交替


ChangeBackgroundColor()
'ChangeBackgroundColor宏
'
'键盘快捷键:Ctrl+Shift+B
将a变暗为整数
a=1
作为整数的Dim c
c=15'灰色
执行While(单元格(a,2)”)
如果(单元格(a,1)”),则“检查新ID”
如果c=15,则
c=2'白色
其他的
c=15'灰色
如果结束
如果结束
行(修剪(Str(a))+“:”+修剪(Str(a))).Interior.ColorIndex=c
a=a+1
环路


End Sub

我已经根据可配置的列,使用RGB值修改了Bartdude的答案,为浅灰色/白色。当值发生变化时,布尔变量被翻转,这用于通过整数值True和False对颜色数组进行索引。2010年为我工作。用图纸编号呼叫sub

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

我在Excel中使用此规则设置交替行的格式:

  • 高亮显示要应用交替样式的行
  • 按“条件格式”->新规则
  • 选择“使用公式确定要格式化的单元格”(最后一项)
  • 以格式值输入规则:
    =MOD(ROW(),2)=0
  • 按“格式”,为交替行设置所需格式,例如填充->颜色
  • 按OK,按OK
  • 如果您希望格式化交替列,请使用
    =MOD(COLUMN(),2)=0


    回答得很好。作为MOD函数的替代,您可以使用
    1-E1
    。所以完整的公式是
    =IF(B2=B1,E1,1-E1)
    在我的例子中,我不能在公式中使用分号(;),Excel只能接受逗号(,)。我使用的是MS Excel v 14.0.7106.5003 32位。是否可以避免使用额外的列?@salman-a,如果没有额外的列,我无法找到解决方案,但您可以隐藏它。回答得很好。根据您的Excel版本,您可能会更幸运地使用以下规则描述公式:=$P2=1I可能误解了,但从我看到的地方和我的测试中,只要A列和B列不为空,这会在每行之间切换颜色。。。所以没有回答这个问题。但我可能遗漏了一些东西……我最初是在08年回答这个问题的,所以如果Office2010与Office2007发生了冲突,我一点也不会感到惊讶。这一次对我很有效。谢谢你。。不过我有一个问题。当我运行这个VBA代码时,它的颜色非常完美,但它也删除了列线/边框。是否可以不删除这些线路?和正义公司
    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