基于值的条件格式化,如果找到值则格式化所有列

Conditional formatting based on a value, if value is found then format all the column

我在 excel 上有这个 table,当找到值 "do" 时,我想给它上色... "hard part"(对我来说)是还为该值以下的行上色,直到第 n.39

这是我现在的照片 table:

到目前为止,这是我用来创建 Table 的代码...

Sub FillCal()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim contatore As Integer
contatatore = -1
Dim StartD As Date, EndD As Date
Dim prova As Integer
Dim rngMerge As Range, rngCell As Range, mergeVal As Range
Dim i As Integer
Dim wks As Worksheet
Dim GiornoSingolo As String
Set wks = ThisWorkbook.Sheets("Foglio1") ' Change Sheet1 to your worksheet

Worksheets("Foglio1").Range("D2:XZ39").Clear

strInterval = "d"
StartD = Foglio1.Cells(2, 2)
EndD = Foglio1.Cells(3, 2)
For Row = 4 To EndD - StartD
    'Cells(4, Row) = Format(StartD + Row - 1, "d mmmm yyyy")
    contatore = DatePart(strInterval, StartD + Row - 1)

    Cells(3, Row).NumberFormat = 0
    Cells(3, Row).Value = contatore
    Cells(3, Row).VerticalAlignment = xlCenter
    Cells(3, Row).HorizontalAlignment = xlCenter
    Cells(3, Row).BorderAround ColorIndex:=1

    GiornoSingolo = Format(StartD + Row - 1, "ddd")

    prova = Application.WorksheetFunction.WeekNum(StartD + Row - 1, 2)

    'Cells(6, Row).NumberFormat = 0
    Cells(4, Row) = Left(GiornoSingolo, 2)
    Cells(4, Row).VerticalAlignment = xlCenter
    Cells(4, Row).HorizontalAlignment = xlCenter
    Cells(4, Row).BorderAround ColorIndex:=1
    'GiornoSingolo = Left(StartD + Row - 1, "ddd")
    'GiornoSingolo = Left(Text(StartD + Row - 1, "ddd"), 1)
    'Cells(6, Row) = Left(StartD + Row - 1, "DDD")
    Cells(2, Row) = Format(StartD + Row - 1, "MMMM' yy")

    Cells(2, Row).BorderAround ColorIndex:=1
    Cells(5, Row).BorderAround ColorIndex:=1
    Cells(6, Row).BorderAround ColorIndex:=1
    Cells(7, Row).BorderAround ColorIndex:=1
    Cells(8, Row).BorderAround ColorIndex:=1
    Cells(9, Row).BorderAround ColorIndex:=1
    Cells(10, Row).BorderAround ColorIndex:=1
    Cells(11, Row).BorderAround ColorIndex:=1
    Cells(12, Row).BorderAround ColorIndex:=1
    Cells(13, Row).BorderAround ColorIndex:=1
    Cells(14, Row).BorderAround ColorIndex:=1
    Cells(15, Row).BorderAround ColorIndex:=1
    Cells(16, Row).BorderAround ColorIndex:=1
    Cells(17, Row).BorderAround ColorIndex:=1
    Cells(18, Row).BorderAround ColorIndex:=1
    Cells(19, Row).BorderAround ColorIndex:=1
    Cells(20, Row).BorderAround ColorIndex:=1
    Cells(21, Row).BorderAround ColorIndex:=1
    Cells(22, Row).BorderAround ColorIndex:=1
    Cells(23, Row).BorderAround ColorIndex:=1
    Cells(24, Row).BorderAround ColorIndex:=1
    Cells(25, Row).BorderAround ColorIndex:=1
    Cells(26, Row).BorderAround ColorIndex:=1
    Cells(27, Row).BorderAround ColorIndex:=1
    Cells(28, Row).BorderAround ColorIndex:=1
    Cells(29, Row).BorderAround ColorIndex:=1
    Cells(30, Row).BorderAround ColorIndex:=1
    Cells(31, Row).BorderAround ColorIndex:=1
    Cells(32, Row).BorderAround ColorIndex:=1
    Cells(33, Row).BorderAround ColorIndex:=1
    Cells(34, Row).BorderAround ColorIndex:=1
    Cells(35, Row).BorderAround ColorIndex:=1
    Cells(36, Row).BorderAround ColorIndex:=1
    Cells(37, Row).BorderAround ColorIndex:=1
    Cells(38, Row).BorderAround ColorIndex:=1
    Cells(39, Row).BorderAround ColorIndex:=1

Next Row



Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

这是输出:

要为 A 列中所有值为 "do" 的单元格及其下方的所有单元格着色,请使用以下公式条件:

=COUNTIF(A:A1,"do")>0

这将自动 'stretch' 向下,因此在单元格 A5 中它将变为 =COUNTIF(A:A5,"do")>0,在单元格 A11 中变为 =COUNTIF(A:A11,"do")>0,依此类推。

然而! 查看您的图像,其中带有 "do" 的行将始终相同,即第 4 行。

因此,select 范围 D4:AE39,并改用以下公式条件:

=(D="do")

随着向右移动, 将更新。但是,由于 $ 符号, 将始终保持为 4

您可以使用此代码(我使用了(for 循环)以获得更好的编码体验):

Sub FillCal()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Dim contatore As Integer
contatatore = -1
Dim StartD As Date, EndD As Date
Dim prova As Integer
Dim rngMerge As Range, rngCell As Range, mergeVal As Range
Dim i As Integer
Dim wks As Worksheet
Dim GiornoSingolo As String
Set wks = ThisWorkbook.Sheets("Foglio1") ' Change Sheet1 to your worksheet

Worksheets("Foglio1").Range("D2:XZ39").Clear

strInterval = "d"
StartD = Worksheets("Foglio1").Cells(2, 2)
EndD = Worksheets("Foglio1").Cells(3, 2)
For Row = 4 To EndD - StartD
    'Cells(4, Row) = Format(StartD + Row - 1, "d mmmm yyyy")
    contatore = DatePart(strInterval, StartD + Row - 1)

    Cells(3, Row).NumberFormat = 0
    Cells(3, Row).Value = contatore
    Cells(3, Row).VerticalAlignment = xlCenter
    Cells(3, Row).HorizontalAlignment = xlCenter
    Cells(3, Row).BorderAround ColorIndex:=1

    GiornoSingolo = Format(StartD + Row - 1, "ddd")

    prova = Application.WorksheetFunction.WeekNum(StartD + Row - 1, 2)

    'Cells(6, Row).NumberFormat = 0
    Cells(4, Row) = Left(GiornoSingolo, 2)
    Cells(4, Row).VerticalAlignment = xlCenter
    Cells(4, Row).HorizontalAlignment = xlCenter
    Cells(4, Row).BorderAround ColorIndex:=1
    'GiornoSingolo = Left(StartD + Row - 1, "ddd")
    'GiornoSingolo = Left(Text(StartD + Row - 1, "ddd"), 1)
    'Cells(6, Row) = Left(StartD + Row - 1, "DDD")
    Cells(2, Row) = Format(StartD + Row - 1, "MMMM' yy")

    For ii = 2 To 39
        Cells(ii, Row).BorderAround ColorIndex:=1
    Next ii

    If Cells(4, Row).Text = "do" Then
        For i = 4 To 39
            Cells(i, Row).Interior.ColorIndex = 9
        Next i
    End If
Next Row



Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub