【问题标题】:Excel VBA Conditional Formating - count the rows of the rangeExcel VBA条件格式 - 计算范围的行数
【发布时间】:2022-03-27 02:54:22
【问题描述】:

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

  1. 颜色;
  2. 边框颜色;
  3. 字体颜色;
  4. 对齐;

很遗憾,我不能使用 TABLE DESIGN...

我的解决方案是......

  1. 当 Row = Even 时,内部颜色类型 A
  2. 当 Row = ODD 时,内部颜色类型为 B
  3. 当 ROW = 1 (HEADER) 时,颜色类型为 C

问题

当范围从第一行开始时,此宏运行正常。

但并非总是选定的范围会从第一行开始,对吧?

这就是问题...

当所选范围从第 2 行开始时,宏需要像这样工作:

  • 标题 = TYPE C = 第 2 行(范围的第一行)
  • 类型 A = 第 3 行(范围的奇数行)
  • 类型 B = 第 4 行(范围的偶数行)

当所选范围从第 3 行开始时,宏需要像这样工作:

  • 标题 = TYPE C = 第 3 行(范围的第一行)
  • 类型 A = 第 4 行(范围的奇数行)
  • 类型 B = 第 5 行(范围的偶数行)

如果有人可以帮助我创建解决方案?

用户VBasic2008的解决方案

如果您想使用您的语言:

  • PAR 表示 EVEN
  • ÍMPAR 表示 ODD
  • LIN 表示 ROW
Sub teal_table()

    Const evenFormula As String = "=PAR(LIN())=LIN()"
    Const oddFormula As String = "=ÍMPAR(LIN())=LIN()"
    
    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:="=LIN()=" & .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

【问题讨论】:

  • 检测第一行(标题)的位置。那么您的条件公式可能取决于ROW()-HEADERROW 的值。如果值为偶数,则类型 A,如果值为奇数,则类型 B(或反向,您的选择)
  • 只需切换一个布尔值boolean = false 即可开始循环boolean = not boolean。我相信这应该有效。现在你有了你的偶数/奇数计数器,只是根据布尔值格式化。

标签: excel vba conditional-formatting


【解决方案1】:

变量条件格式(带状行)

  • 如有必要,将ODD, EVEN, and ROW 替换为ÍMPAR, PAR, and LIN。是吗?

守则

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

【讨论】:

  • 谢谢@VBasic2008 !!!不幸的是不工作!所有线条仍然是白色的
  • 您是否(必须)根据您的语言替换所有出现的ODD, EVEN, and ROW?我的意思是,如果它不起作用,我不会发布它。再次检查,它仍然有效。
【解决方案2】:

您可以使用 ROWS 代替 ROW。

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

【讨论】:

  • 嗨@norie。不幸停在这里.FormatConditions.Add Type:=xlExpression, Formula1:="=ISEVEN(ROWS(R" & Selection.Row & "C:RC))"
  • 您使用的是哪个版本的 Excel?
  • 我正在使用 Office 365(葡萄牙语-BR)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多