我不太了解VBA。 Ivesleo回答了一个名为“根据价值变化整理整个专栏”的帖子。 Link to Shade Row Post
我使用了他的代码并且它有效但我不知道如何更改代码以根据我的需要修改代码。
周/城市
W1N1 Silverton W1N1 Silverton W2N3 Silverton W1N4 Silverton W1N4 Piley
Sub color()
'Replace the 2 values of G = and C= by the number of the column containing _
'the values being referenced.
Dim g As Long
Dim c As Integer
Dim colorIt As Boolean
g = 2
c = 2
colorIt = True
Do While Cells(g, c) <> ""
test_value = Cells(g, c)
Do While Cells(g, c) = test_value
If colorIt Then
Cells(g, c).EntireRow.Select
Selection.Interior.ColorIndex = 15
Else
Cells(g, c).EntireRow.Select
Selection.Interior.ColorIndex = x1None
End If
g = g + 1
Loop
colorIt = Not (colorIt)
Loop
End Sub
答案 0 :(得分:1)
以下代码专为您的情况量身定制:
Sub color()
Dim r As Long
Dim colourIt As Boolean
Dim colour As XlColorIndex
colourIt = False
With ActiveSheet
r = 2 ' First row of data
Do While .Cells(r, "B").Value <> ""
'See if value has changed
If .Cells(r, "B").Value <> .Cells(r - 1, "B").Value Or _
.Cells(r, "C").Value <> .Cells(r - 1, "C").Value Then
colourIt = Not colourIt
End If
'Determine which colour to use on this row
If colourIt Then
colour = 15
Else
colour = xlColorIndexNone
End If
'Apply the colouring
.Range(.Cells(r, "B"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.ColorIndex = colour
'Point to the next row of data
r = r + 1
Loop
End With
End Sub
注意:变量colourIt
(是colorIt
,但我是澳大利亚人,所以我将使用英文拼写!!)只是一个布尔变量,因此可以采用值True
或False
。每次测试标准发生变化时,代码都会在两个可能的值之间切换(使用代码colourIt = Not colourIt
)。
海报编辑:非常感谢你。我只想在这里添加如何更改它以进一步适应我的情况。我改变它如下:我使用RGB调色板将颜色更改为浅橙色(我猜这就是所谓的)并且我将.Range(.Cells(r,“B”)等更改为.Range(。单元格(r,“A”)等b / c突出显示从B列开始的行与从A列开始的行。
我将在评论中再发帖谢谢。
'Determine which colour to use on this row
If colourIt Then
colour = RGB(252, 228, 214)
Else
colour = xlColorIndexNone
End If
'Apply the colouring
.Range(.Cells(r, "A"), .Cells(r, .Cells(r, .Columns.Count).End(xlToLeft).Column)).Interior.color = colour