Excel VBA条件格式-计算范围内的行数

Excel VBA条件格式-计算范围内的行数,excel,vba,conditional-formatting,Excel,Vba,Conditional Formatting,对于我使用的每一张纸,我都必须应用特定的设计 色彩 边框颜色 字体颜色 对齐 不幸的是,我不能使用表设计 我的解决办法是 当行=偶数时,则内饰颜色类型为A 当行=奇数时,则为内饰颜色类型B 当行=1(标题)时,则颜色类型为C 问题 当范围从第一行开始时,此宏运行正常 但所选范围并不总是从第一行开始,对吗 这就是问题所在 当选定范围在第2行开始时,宏需要如下操作: 标题=类型C=第2行(范围的第一行) A型=第3行(范围的奇数行) 类型B=第4行(范围的偶数行) 当选定范围在第3行开始时,宏需

对于我使用的每一张纸,我都必须应用特定的设计

  • 色彩
  • 边框颜色
  • 字体颜色
  • 对齐 不幸的是,我不能使用表设计

    我的解决办法是

  • 当行=偶数时,则内饰颜色类型为A
  • 当行=奇数时,则为内饰颜色类型B
  • 当行=1(标题)时,则颜色类型为C
  • 问题 当范围从第一行开始时,此宏运行正常

    但所选范围并不总是从第一行开始,对吗

    这就是问题所在

    当选定范围在第2行开始时,宏需要如下操作:

    • 标题=类型C=第2行(范围的第一行)
    • A型=第3行(范围的奇数行)
    • 类型B=第4行(范围的偶数行)
    当选定范围在第3行开始时,宏需要如下操作:

    • 标题=类型C=第3行(范围的第一行)
    • A型=第4行(范围的奇数行)
    • 类型B=第5行(范围的偶数行)
    如果有人能帮我创建一个解决方案

    我的VBA新手解决方案
    Sub-linhas()
    '将对齐和边框设置为选定范围
    有选择
    .HorizontalAlignment=xlCenter
    .垂直对齐=xlCenter
    .Borders.LineStyle=xlContinuous
    .Borders.Color=RGB(100100100)
    .Borders.TintAndShade=0
    .Borders.Weight=xlThin
    以
    '为行应用颜色=ENEN
    Selection.FormatConditions.add类型:=xlExpression,公式1:=“=PAR(LIN())=LIN()
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    带Selection.FormatConditions(1)
    .Interior.Color=RGB(255、255、255)
    .Font.Color=RGB(0,0,0)
    以
    Selection.FormatConditions(1).StopIfTrue=False
    '为行应用颜色=ENEN
    Selection.FormatConditions.add类型:=xlExpression,公式1:=“=ÍMPAR(LIN())=LIN()
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    带Selection.FormatConditions(1)
    .Interior.Color=RGB(245245245245)
    .Font.Color=RGB(0,0,0)
    以
    Selection.FormatConditions(1).StopIfTrue=False
    '将颜色应用于标题
    Selection.FormatConditions.add类型:=xlExpression,公式1:=“=LIN()=1”
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    带Selection.FormatConditions(1)
    .Interior.Color=RGB(0、128、128)
    .Font.Color=RGB(255、255、255)
    .Font.Bold=True
    以
    Selection.FormatConditions(1).StopIfTrue=False
    端接头
    
    可变条件格式(带状行)
    • 如果需要,替换<代码>奇数、偶数和行< <代码> >代码>是吗
    代码

    Option Explicit
    
    Sub linhas()
    
        Const evenFormula As String = "=EVEN(ROW())=ROW()"
        Const oddFormula As String = "=ODD(ROW())=ROW()"
        
        If TypeName(Selection) <> "Range" Then Exit Sub
        
        With Selection
            
            ' Set aligment and border to Selected Range
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            .Borders.LineStyle = xlContinuous
            .Borders.Color = RGB(100, 100, 100)
            .Borders.TintAndShade = 0
            .Borders.Weight = xlThin
            
            Dim oFormula As String, eFormula As String
            If .Row Mod 2 = 0 Then
                eFormula = evenFormula
                oFormula = oddFormula
            Else
                eFormula = oddFormula
                oFormula = evenFormula
            End If
            
            .FormatConditions.Delete
            
            'Apply colors for ROW = EVEN
            .FormatConditions.Add Type:=xlExpression, Formula1:=eFormula
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                .Interior.Color = RGB(255, 255, 255)
                .Font.Color = RGB(0, 0, 0)
                .StopIfTrue = False
            End With
     
            ' Apply colors for ROW = ODD
            .FormatConditions.Add Type:=xlExpression, Formula1:=oFormula
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                .Interior.Color = RGB(245, 245, 245)
                .Font.Color = RGB(0, 0, 0)
                .StopIfTrue = False
            End With
    
            ' Apply colors to HEADER
            .FormatConditions.Add Type:=xlExpression, Formula1:="=ROW()=" & .Row
            With .FormatConditions(.FormatConditions.Count)
                .SetFirstPriority
                .Interior.Color = RGB(0, 128, 128)
                .Font.Color = RGB(255, 255, 255)
                .Font.Bold = True
                .StopIfTrue = False
            End With
    
        End With
    
    End Sub
    
    选项显式
    副林哈斯()
    Const evenFormula As String=“=偶数(行())=行()”
    Const oddFormula As String=“=奇数(行())=行()”
    如果TypeName(选择)“Range”,则退出Sub
    有选择
    '将对齐和边框设置为选定范围
    .HorizontalAlignment=xlCenter
    .垂直对齐=xlCenter
    .Borders.LineStyle=xlContinuous
    .Borders.Color=RGB(100100100)
    .Borders.TintAndShade=0
    .Borders.Weight=xlThin
    将公式的尺寸标注为字符串,将公式的尺寸标注为字符串
    如果.Row Mod 2=0,则
    eFormula=均匀公式
    oFormula=Odd公式
    其他的
    eFormula=odd公式
    oFormula=公式
    如果结束
    .FormatConditions.Delete
    '为行应用颜色=偶数
    .FormatConditions.Add类型:=Xexpression,公式1:=eFormula
    使用.FormatConditions(.FormatConditions.Count)
    .SetFirstPriority
    .Interior.Color=RGB(255、255、255)
    .Font.Color=RGB(0,0,0)
    .StopIfTrue=False
    以
    '为行应用颜色=奇数
    .FormatConditions.Add类型:=Xexpression,公式1:=oFormula
    使用.FormatConditions(.FormatConditions.Count)
    .SetFirstPriority
    .Interior.Color=RGB(245245245245)
    .Font.Color=RGB(0,0,0)
    .StopIfTrue=False
    以
    '将颜色应用于标题
    .FormatConditions.Add类型:=xlExpression,公式1:=“=ROW()=”&.ROW
    使用.FormatConditions(.FormatConditions.Count)
    .SetFirstPriority
    .Interior.Color=RGB(0、128、128)
    .Font.Color=RGB(255、255、255)
    .Font.Bold=True
    .StopIfTrue=False
    以
    以
    端接头
    
    您可以使用行而不是行

    With Selection
    
        .FormatConditions.Delete
    
        'Apply colors for ROW = EVEN
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ISEVEN(ROWS(R" & Selection.Row & "C:RC))"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            .Interior.Color = RGB(255, 255, 255)
            .Font.Color = RGB(0, 0, 0)
        End With
        .FormatConditions(1).StopIfTrue = False
    
        ' Apply colors for ROW = ODD
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ISODD(ROWS(R" & Selection.Row & "C:RC))"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            .Interior.Color = RGB(245, 245, 245)
            .Font.Color = RGB(0, 0, 0)
        End With
        .FormatConditions(1).StopIfTrue = False
    
        ' Apply colors to HEADER
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ROWS(R" & Selection.Row & "C:RC)=1"
        With .FormatConditions(.FormatConditions.Count)
            .SetFirstPriority
            .Interior.Color = RGB(0, 128, 128)
            .Font.Color = RGB(255, 255, 255)
            .Font.Bold = True
        End With
        .FormatConditions(1).StopIfTrue = False
    
    End With
    

    检测第一行(标题)的位置。然后,条件公式可能取决于
    ROW()-HEADERROW
    的值。如果值为偶数,则键入A;如果值为奇数,则键入B(或相反,由您选择),只需切换布尔值
    boolean=false
    ,然后在循环中启动
    boolean=not boolean
    。我认为这应该行得通。现在您有了偶数/奇数计数器,只需根据布尔值进行格式化。谢谢@vbasic208!!!不幸的是,你不工作!所有行仍为白色您是否(必须)根据您的语言替换所有出现的
    奇数、偶数和行
    ?我是说,如果它不起作用,我就不会发了。再次检查,它仍然有效。有效!!!TKS!!!嗨,诺莉。不幸的是,这里停止了。FormatConditions.Add Type:=xlExpression,Formula1:=“=ISEVEN(ROWS(R“&Selection.Row&“C:RC))”您使用的是哪个版本的Excel?我使用的是office 365(葡萄牙语BR)