颜色格式化每隔一行

我能够find堆栈溢出两个代码,但没有得到它给一个浅灰色格式的行,其次是白色的行,帮助将不胜感激,尝试两个代码

Sub Colour(rng As Range, firstColor As Long, secondColor As Long) rng.Interior.ColorIndex = xlAutomatic rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0" rng.FormatConditions(1).Interior.Color = firstColor rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)<>0" rng.FormatConditions(2).Interior.Color = secondColor End Sub 'Usage: ' Sub ColourFormatting() Dim rng As Range Dim firstColor As Long Dim secondColor As Long Set rng = Range("A1:E10") firstColor = Pattern = xlSolid: PatternColorIndex = xlAutomatic: ThemeColor = xlThemeColorDark1: TintAndShade = -0.149998474074526: PatternTintAndShade = 0 secondColor = TintAndShade = 0: PatternTintAndShade = 0 ' Call Colour(rng, firstColor, secondColor) End Sub Sub ShadeEveryOtherRow() Dim Counter As Integer 'For every row in the current selection... For Counter = 1 To Range("A1:E30").Rows.Count 'If the row is an odd number (within the selection)... If Counter Mod 2 = 1 Then 'Set the pattern to xlGray16. Range("A1:E30").Rows(Counter).Interior.Pattern = xlSolid: PatternColorIndex = xlAutomatic: ThemeColor = xlThemeColorDark1: TintAndShade = -0.149998474074526: PatternTintAndShade = 0 End If Next End Sub 

你不需要VBA – 你可以使用条件格式与公式:

 =MOD(ROW(),2)=1 

并将格式设置为浅灰色。 对第一行执行此操作,然后将该行作为格式复制到该区域(特殊粘贴)。

然而,你的代码在参数中使用了错误的分隔符,VBA中的冒号意味着新行,所以你的语句

 Range("A1:E30").Rows(Counter).Interior.Pattern = xlSolid: PatternColorIndex = xlAutomatic 

是相同的

 Range("A1:E30").Rows(Counter).Interior.Pattern = xlSolid PatternColorIndex = xlAutomatic 

我认为VBA正在使用你的最新的select第二行。

尝试:

 Range("A1:E30").Rows(Counter).Interior.Pattern = xlSolid, PatternColorIndex = xlAutomatic 

等等