我正在尝试在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
答案 0 :(得分: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