如何根据列1中的值更改行颜色?

我想知道如何根据列1中的值更改多行的行颜色。可以说在A1到A5我有值“100”和A6到A10我有值“150”,我想要能够将第1行到第5行的颜色更改为蓝色,因为由于值“150”,A1到A5的值为“100”,而A6到A10的值为另一种颜色。 几乎我需要改变颜色相同,如果价值是相同的。 我的代码可以工作,但是每次值改变时它都会变成全蓝色而不是不同的颜色。

编辑答案:

Dim i As Long Dim holder As String Set UsedRng = ActiveSheet.UsedRange FirstRow = UsedRng(1).Row LastRow = UsedRng(UsedRng.Cells.Count).Row r = WorksheetFunction.RandBetween(0, 255) g = WorksheetFunction.RandBetween(0, 255) b = WorksheetFunction.RandBetween(0, 255) holder = Cells(FirstRow, 1).Value For i = FirstRow To LastRow '<--| loop through rows index myColor = RGB(r, g, b) If Cells(i, 1).Value = holder Then Cells(i, 1).EntireRow.Interior.Color = myColor Else holder = Cells(i, 1).Value r = WorksheetFunction.RandBetween(0, 255) g = WorksheetFunction.RandBetween(0, 255) b = WorksheetFunction.RandBetween(0, 255) Cells(i, 1).EntireRow.Interior.Color = RGB(r, g, b) End If Next i 

我build议当值更改循环时做随机颜色:

 Sub Color() lastrow = ActiveSheet.UsedRange.Rows.Count For i = 2 To lastrow If Cells(i, 1).Value = Cells(i - 1, 1).Value Then r = WorksheetFunction.RandBetween(0, 255) g = WorksheetFunction.RandBetween(0, 255) b = WorksheetFunction.RandBetween(0, 255) Cells(i, 1).Interior.Color = RGB(r, g, b) Else Cells(i, 1).Interior.Color = RGB(r, g, b) End If Next i End Sub 

结果将如下所示:

在这里输入图像说明

你可以从这个代码开始

 Sub main() Dim myCol As Long, i As Long For i = 1 To 10 '<--| loop through rows index With Cells(i, 1) '<--| reference cell at row i and column 1 Select Case .value Case 100 myCol = vbBlue Case 150 myCol = vbRed Case Else myCol = vbWhite End Select .EntireRow.Interior.Color = myCol End With Next i End Sub 

这是如何检查单元格A1到A10的值为100,如果所有单元格都包含100,则使用蓝色绘制从1到10的所有行。

 Sub ColorMeBlue() Dim iStart, iEnd As Long Dim i As Integer Dim b As Boolean iStart = 1: iEnd = 10 b = False '~~> We will set b to true if all cells in A1:A10 conatins 100 For i = iStart To iEnd If Cells(i, 1) = 100 Then b = True End If Next '~~> We will paint Blue if b is true If b Then Rows("1:10").Interior.Color = vbBlue End If End Sub 

您可以使用相同的逻辑来设置下一组行。

我没有把整个代码的原因是,你可以自己练习。

根据你对我评论的回复,我假设你不知道第一列的确切数值,也不知道有多less不同的数值。

为了让我的答案不太复杂,我进一步假定第一列只包含非负数。 如果不是这种情况,则只需将列中的数据types映射到该数字范围即可。

在上面的假设下,你可以使用下面的代码。

 Public Sub SetRowColorBasedOnValue() Dim firstColumn As Range Set firstColumn = ActiveSheet.UsedRange.Columns(1) Dim minValue As Double Dim maxValue As Double minValue = Application.Min(firstColumn) maxValue = Application.Max(firstColumn) Dim cell As Range Dim shade As Double For Each cell In firstColumn.Cells If Not IsEmpty(cell) Then shade = (CDbl(cell.Value2) - minValue) / (maxValue - minValue) SetRowColorToShade cell, shade End If Next End Sub Private Sub SetRowColorToShade(ByVal cell As Range, ByVal shade As Double) With cell.EntireRow.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = shade .PatternTintAndShade = 0 End With End Sub 

无可否认,颜色可以非常相似。 如果您使用的是Excel 2013或更高版本,则可以使用cell.EntireRow.Interior.Color = HSL(hue,saturation,chroma)而不是设置色调和阴影来根据值更改色调。 这提供了更多不同的颜色。