根据数字对其他列的行进行颜色设置 [英] Color rows depending on another columns by numbers

查看:25
本文介绍了根据数字对其他列的行进行颜色设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改以下代码。 从1张到8张,也许更少。 表1=A列包含数字,B列包含将A列分组的数字。 A栏B栏 11200 3 11202 3 12500 4 12502 4 列B中的偶数行=蓝色,奇数列B=绿色 我需要应用更多的颜色,所以我需要IS EVEN+1=黄色,ISODD+1=棕色。

    Sub Color()

Dim CvbRed, cYellow, cGreen, cBlue As Integer

For Each cell In Range("B5:B" & Range("A" & Rows.Count).End(xlUp).Row)

 Select Case Color
 
Case IsEven
Range("A5:A").Cells.Interior.Color = vbRed
        cRed = cRed + 1

Case IsOdd
Range("A5:A").Cells.Interior.Color = vbYellow
        cYellow = cYellow + 1
  
Case IsEven + 2
Range("A5:A").Cells.Interior.Color = vbGreen
        cGreen = cGreen + 1
    
Case IsOdd + 2
Range("A5:A").Cells.Interior.Color = vbBlue
        cBlue = cBlue + 1
    
    End Select
Next cell
End Sub

请查看本帖上图,A栏只需要颜色,具体取决于B栏何时有偶数、奇数、偶数+1、奇数+1。

推荐答案

使用Mod

Option Explicit
Sub ColorMacro()

    Dim wb As Workbook, cell As Range, lastrow As Long
    Dim n As Integer, i As Integer
    Dim arColor ' odd-green, even-blue, odd+1-brown, even+1-yellow
    arColor = Array(RGB(128, 255, 128), _
              RGB(128, 128, 255), _
              RGB(200, 150, 100), _
              RGB(255, 255, 128))
     
    Set wb = ThisWorkbook
    For n = 2 To wb.Sheets.Count
        With wb.Sheets(n)
            lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
            For Each cell In .Range("B2:B" & lastrow)
                i = (cell.Value - 1) Mod 4
                cell.Offset(,-1).Interior.Color = arColor(i)
            Next
        End With
    Next
    
End Sub

这篇关于根据数字对其他列的行进行颜色设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆