Excel,比较区域中的单元格并相应地更改颜色

x33g5p2x  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(263)

我一直在努力解决这个问题,但我总是做不到!
实际上,我有一个命名的单元格区域,我希望检查该区域中的每个单元格是否大于上面的单元格,并在发生这种情况时更改其内部颜色,或者如果单元格的值相同,则相应地设置颜色。要实现如下所示的结果(忽略灰线)

我遇到的问题是让这个工作,因为命名的范围在每次刷新时都会改变!所以我不能在中编码静态单元格,它们必须每次都不同!
有什么想法吗?
我什么没试过...?哈哈
if函数,单元格偏移量,你能想到的!也许我只需要提高我的编码游戏:D

Dim Col0 As Long
Dim Col1 As Long
Dim Col2 As Long
Dim Col3 As Long
Dim Col4 As Long
Dim Col5 As Long
Dim Col6 As Long
Dim Col7 As Long

c = 0

Col0 = RGB(142, 169, 219)
Col1 = RGB(213, 184, 234)
Col2 = RGB(255, 217, 102)
Col3 = RGB(169, 208, 142)
Col4 = RGB(244, 176, 132)
Col5 = RGB(180, 238, 210)
Col6 = RGB(208, 215, 145)
Col7 = RGB(167, 127, 225)

WS.Range("C9").Interior.Color = Col1

Dim tms As Range
pr = 0
pr = ActiveSheet.Range("B15", ActiveSheet.Cells(Rows.Count, "B").End(xlUp)).Count
Set tms = Application.Range("B15:B" & 14 + pr)

Dim First As Integer
Dim Last As Integer

First = 15
Last = 15 + pr - 1

Debug.Print First
Debug.Print Last

If First <> Last Then
    WS.Range("B" + First).Interior.Color = Col1
Else
    Cells(k, 3).Value = "Same"
End If

Set Cel1 = WS.Range("B15")

'Col0 = "14395790"
'Col1 = "15382741"
'Col2 = "6740479"
'Col3 = "9359529"
'Col4 = "8696052"
'Col5 = "13823668"
'Col6 = "9557968"
'Col7 = "14778279"

t = 0

'MsgBox pr




'tms.Interior.Color = Col1

'For Each cel In tms.Cells

'If Cel1.Value < Cel2.Value

'    Cel1.Interior.Color = "Col" & c
'    c = c + 1
'    End If

't = t + 1

'Next cel
'On Error Resume Next
cs7cruho

cs7cruho1#

Mike,你的思路是对的,但是你没有在时间不变的情况下保持当前颜色可用于后面的行。请注意颜色数组的用法。使用数组,你可以通过数字索引(代码中的'ColorIndex')轻松访问元素。

Private Sub CommandButton1_Click()
    Dim ws As Worksheet
    Set ws = Worksheets(1)
    
    'Dim c As Long
    Dim CurrRow As Long     'The row we are currently evaluating
    Dim PrevRow As Long     'The previous row (act. first of current time)
    Dim CurrColor As Long   'The color index currently in use
    Dim ColorArr As Variant 'Array of colors
    Dim ColorIndex As Integer
    
    Dim Color0 As Long
    Dim Color1 As Long
    Dim Color2 As Long
    Dim Color3 As Long
    Dim Color4 As Long
    Dim Color5 As Long
    Dim Color6 As Long
    Dim Color7 As Long
    
    'Define the colors
    Color0 = RGB(142, 169, 219)
    Color1 = RGB(213, 184, 234)
    Color2 = RGB(255, 217, 102)
    Color3 = RGB(169, 208, 142)
    Color4 = RGB(244, 176, 132)
    Color5 = RGB(180, 238, 210)
    Color6 = RGB(208, 215, 145)
    Color7 = RGB(167, 127, 225)
    
    'Define an array of those colors
    ColorArr = Array(Color0, Color1, Color2, Color3, Color4, Color5, Color6, Color7)
    
    'Initial index that we will use
    ColorIndex = 0
    
    'Test of the color ?
    ws.Range("C9").Interior.Color = ColorArr(ColorIndex)
    
    Dim pr As Long
    Dim tms As Range
    
    pr = 0
    pr = ActiveSheet.Range("B15", ActiveSheet.Cells(Rows.Count, "B").End(xlUp)).Count
    Set tms = Application.Range("B15:B" & 14 + pr)
    
    Dim First As Integer
    Dim Last As Integer
    
    First = 15
    Last = 15 + pr - 1
    
    Debug.Print First
    Debug.Print Last
    
    
    'Traverse the cells with the time stamps
    'Color the first cell with the initial fill color
    'Following cells are colored with the same color ...
    '... if the time in cell is the same as previous
    'If the time in the cell is different ...
    '  - update PrewRow to the current
    '  - update ColorIndex
    'Finally, fill cell
    '
    PrevRow = First
    For CurrRow = First To Last
        If CurrRow = First Then
            Cells(CurrRow, 2).Interior.Color = ColorArr(ColorIndex)
        Else

            'Check if time is different from previous
            If Cells(CurrRow, 2).Value <> Cells(PrevRow, 2).Value Then
                PrevRow = CurrRow
                ColorIndex = ColorIndex + 1
            End If

            'Fill cell
            Cells(CurrRow, 2).Interior.Color = ColorArr(ColorIndex)
        End If
    Next
    
End Sub

相关问题