簡體   English   中英

在范圍左側查找每個單元格

[英]Look for each cells on the left of the range

因此,我試圖使我的宏在范圍列的左側查找每個單元格,如果它不為空且已過期/過期(請參見Formula1天),它將變為紅色。

目前,我的代碼尚未優化,但是我是VBA編碼的新手。 我已經按照上面的邏輯編寫了它。

    Sub Macro1()
'
' Macro1 Macro
' This macro is to highlight the cell if it has been more than 5 days
'
' Keyboard Shortcut: Ctrl+b
'

' this is the range of cells
Range("L4:L1000").Select

' this targets any cell value within that range that is above the Formula1 variable which can be adjusted
    For Each cell In Range("L4:L1000")
        If Not IsEmpty(Range("L4:L1000").Offset(0, -1).Value) Then

            Selection.FormatConditions.Delete
            Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="6"

' this selects which colour you want to highlight the targetted cells to
            Selection.FormatConditions(1).Interior.ColorIndex = 3

            ActiveWindow.SmallScroll Down:=0

        End If
    Next

Range("N4:N1000").Select

    For Each cell In Range("N4:N1000")
        If Not IsEmpty(Range("N4:N1000").Offset(0, -1).Value) Then

            Selection.FormatConditions.Delete
            Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="5"

            Selection.FormatConditions(1).Interior.ColorIndex = 3

            ActiveWindow.SmallScroll Down:=0

        End If
    Next

Range("Q4:Q1000").Select

    For Each cell In Range("Q4:Q1000")
        If Not IsEmpty(Range("Q4:Q1000").Offset(0, -1).Value) Then

            Selection.FormatConditions.Delete
            Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="4"

            Selection.FormatConditions(1).Interior.ColorIndex = 3

            ActiveWindow.SmallScroll Down:=0

        End If
    Next

Range("S4:S1000").Select

    For Each cell In Range("S4:S1000")
        If Not IsEmpty(Range("S4:S1000").Offset(0, -1).Value) Then

            Selection.FormatConditions.Delete
            Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="2"

            Selection.FormatConditions(1).Interior.ColorIndex = 3

            ActiveWindow.SmallScroll Down:=0

        End If
    Next

End Sub

現在我的問題是,這會使該列中的所有空白單元格變為紅色,如果一個單元格晚了,它將使該范圍內的下一個范圍單元格(該行)變為紅色,而我只希望第一個遲到的單元格變為紅色。

我是否假設如果左側的單元格為空白,那么您是否希望將其着色為紅色是否正確?

如果是這樣,我將使用它(例如,需要着色的列是B列):

dim n as integer
Range("B2").select
n= range(selection,selection.end(xldown)).count

For i=1 to n
If isblank(cells(i,1)) then
     cell(i,2).select
     Selection.FormatConditions.Delete
     Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="2"
     Selection.FormatConditions(1).Interior.ColorIndex = 3
Endif
next i

如果我誤會了,請告訴我。 沒有更多的聲譽,我無法發表評論。

條件格式的工作方式與大多數VBA單元編程有所不同。

您可以一次性將條件格式應用於大量單元格。

這是重寫代碼第一部分的示例。 使用Range(“ L4:L1000”):

Sub testing()
    With Range("L4:L1000")
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
            Formula1:="=AND(NOT(ISBLANK(K4)), L4>5)"
        .FormatConditions(1).Interior.ColorIndex = 3
    End With End Sub

請注意,條件格式公式的編寫方式就像僅用於第一個單元格一樣。 單元格L4。 條件公式被“拖動”以在Excel中應用。

您可以通過更改范圍和公式等來對其進行修改以在工作表中應用其他條件公式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM