0

我正在尝试在 excel 宏中找到可以解决以下问题的代码。

如果第一列包含任何文本,则用某种颜色突出显示第一列单元格,直到下一个文本不出现在同一列中。当任何文本出现在同一列中时,开始用不同的颜色为单元格着色。

我必须对工作簿中的所有工作表重复此操作。谢谢。

在此处输入图像描述

现在我正在使用这个宏来为空的单元格着色,但问题是每当遇到文本时颜色都不会改变

Sub try()
Dim i As Integer
Dim j As Integer
Dim k As Integer
i = 200
j = 100
k = 5

Application.ScreenUpdating = False

With ActiveSheet.UsedRange
    .AutoFilter Field:=1, Criteria1:=""
    If WorksheetFunction.CountBlank(.Columns(1)) > 0 Then
        .Columns(1).SpecialCells(xlCellTypeBlanks).Interior.Color = RGB(i, j, k)
    Else
        i = i - 50
        j = j - 10
        k = 255
    End If

    .AutoFilter
End With

Application.ScreenUpdating = True

End Sub
4

1 回答 1

1

干得好:

Option Explicit

Sub Color_Ranges()

Dim oSheet                  As Worksheet
Dim oRange                  As Range
Dim oRange_Color            As Range
Dim oBaseCell               As Range
Dim lLast_Row               As Long
Dim lRange_Rows             As Long
Dim iCnt_Values             As Integer
Dim iCnt_Intervals          As Integer

Dim r                       As Integer
Dim g                       As Integer
Dim b                       As Integer

Dim iCnt                    As Integer


Set oSheet = ThisWorkbook.Sheets(1)
With oSheet
    lLast_Row = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
End With

'Total range you want to color
Set oRange = oSheet.Range(Cells(1, 1), Cells(lLast_Row, 1))
lRange_Rows = oRange.Rows.Count

'Count values
iCnt_Values = WorksheetFunction.CountA(oRange)
'Count intervals
iCnt_Intervals = iCnt_Values - 1

'Generate random colors
r = CInt(Int((255 * Rnd()) + 1))
g = CInt(Int((255 * Rnd()) + 1))
b = CInt(Int((255 * Rnd()) + 1))

Set oBaseCell = oRange.Cells(1, 1)
For iCnt = 1 To iCnt_Intervals
    Set oRange_Color = Range(oBaseCell, oBaseCell.End(xlDown))
    oRange_Color.Interior.Color = RGB(r, g, b)
    r = CInt(Int((255 * Rnd()) + 1))
    g = CInt(Int((255 * Rnd()) + 1))
    b = CInt(Int((255 * Rnd()) + 1))
    Set oBaseCell = oBaseCell.End(xlDown)
    Set oRange_Color = Nothing
Next iCnt

End Sub
于 2012-10-25T14:12:11.583 回答