Excel VBA 条件格式 - 计算范围的行数
Excel VBA Conditional Formating - count the rows of the range
对于我使用的每个 sheet,我都必须应用特定的设计。
- 颜色;
- 边框颜色;
- 字体颜色;
- 对齐;
不幸的是,我无法使用 TABLE DESIGN...
我的解决方案是...
- 当行=偶数时,则内部颜色类型A
- 当Row = ODD时,则内部颜色类型B
- 当ROW = 1 (HEADER)时,则颜色类型为C
问题
当范围从第一行开始时,此宏运行正常。
但并非总是选择的范围会从第一行开始,对吗?
这就是问题所在...
当所选范围 从第 2 行 开始时,宏需要像这样工作:
- Header = TYPE C = 第 2 行(范围的第一行)
- 类型 A = 第 3 行(范围的奇数行)
- 类型 B = 第 4 行(范围的偶数行)
当所选范围 从第 3 行开始时 宏需要像这样工作:
- Header = 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
可变条件格式(带状行)
- 如有必要,将
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
您可以使用 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
对于我使用的每个 sheet,我都必须应用特定的设计。
- 颜色;
- 边框颜色;
- 字体颜色;
- 对齐;
不幸的是,我无法使用 TABLE DESIGN...
我的解决方案是...
- 当行=偶数时,则内部颜色类型A
- 当Row = ODD时,则内部颜色类型B
- 当ROW = 1 (HEADER)时,则颜色类型为C
问题
当范围从第一行开始时,此宏运行正常。
但并非总是选择的范围会从第一行开始,对吗?
这就是问题所在...
当所选范围 从第 2 行 开始时,宏需要像这样工作:
- Header = TYPE C = 第 2 行(范围的第一行)
- 类型 A = 第 3 行(范围的奇数行)
- 类型 B = 第 4 行(范围的偶数行)
当所选范围 从第 3 行开始时 宏需要像这样工作:
- Header = 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
可变条件格式(带状行)
- 如有必要,将
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
您可以使用 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