[英]Have conditional formatting macro skip blanks in Excel
我已經在以下使用記錄器宏:
Application.ScreenUpdating = False
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0", Formula2:="=19.5"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = False
.Italic = True
.ColorIndex = 4
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=19.6", Formula2:="=34.4"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = False
.Italic = True
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.499984740745262
End With
Selection.FormatConditions(1).StopIfTrue = False
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.FormatConditions(1).StopIfTrue = False
然后,我使用宏剪切所有條件,僅保留格式。 但是,不管我做什么,Isblank都添加了另一個條件格式設置條件,使其只能在非空白中運行,在條件格式設置宏之后,格式為綠色(它將任何0-19.5變為綠色,但單元格中沒有任何內容)。
有沒有一種方法可以向此宏添加跳過行? 如果為空,我希望它移至下一個單元格。 我沒有設置范圍,所以這就是所有選擇的原因。
您可以循環選擇中的每個單元格,並且僅在單元格不為空白時才應用格式。
Option Explicit
Sub test()
Dim cel As Range
Application.ScreenUpdating = False
On Error Resume Next
For Each cel In Selection
If cel <> "" Then
cel.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=0", Formula2:="=19.5"
cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
With cel.FormatConditions(1).Font
.Bold = False
.Italic = True
.ColorIndex = 4
End With
cel.FormatConditions(1).StopIfTrue = True
cel.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=19.6", Formula2:="=34.4"
cel.FormatConditions(cel.FormatConditions.Count).SetFirstPriority
With cel.FormatConditions(1).Font
.Bold = False
.Italic = True
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.499984740745262
End With
cel.FormatConditions(1).StopIfTrue = False
With cel
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
cel.FormatConditions(1).StopIfTrue = False
End If
Next cel
Application.ScreenUpdating = True
End Sub
如果只需要格式化,則使用此處記錄的宏,然后簡單地復制導致格式化的代碼。
With Selection.FormatConditions(1).Font
.Bold = False
.Italic = True
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.499984740745262
End With
此代碼塊似乎具有您可能需要的某些格式。
另外,由於您正在處理選擇內容而不是單獨處理每個單元格,因此檢查空白單元格將更加困難。 據我所知,您無法做到,因為您將選擇范圍視為一個整體。 單元格也是一個范圍。 如果我錯了,有人糾正我。
我遇到了類似的問題,我自己使用條件格式設置了條件,即單元格為空白或具有字符串值,在此我希望忽略前者。
我發現條件格式設置函數不能與ADDRESS()一起正常工作,但是我可以編寫用戶定義的函數與= AND()一起使用,從而解決了這個問題。
Public Function CellContent() As Variant
On Error Resume Next
CellContent = Application.Caller.value
End Function
Public Function Cell_HasContent() As Boolean
On Error Resume Next
If Application.Caller.value = "" Then
Cell_HasContent = False
Else
Cell_HasContent = True
End If
End Function
Resume Next語句很關鍵。 如果您要檢查所有非空白值是否為2,則可以使用以下方法進行檢查:
Formula1:="=AND(CellContent()=2,CellHasContent())"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.