簡體   English   中英

非空白的 Excel 2010 VBA 條件格式

[英]Excel 2010 VBA Conditional Formatting on Non-Blanks

所以我正在制作一個由 Access 中的許多工作表組成的 excel 工作簿。 我只想在非空白單元格上應用網格線(或邊框)。

通過 excel 手動執行它是微不足道的,但是當我將宏 i 記錄器應用於我的代碼時,它似乎不起作用。

有什么想法嗎?

Sub Macro1()

ActiveWindow.DisplayGridlines = False
Cells.Select

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=LEN(TRIM(A1))>0"

Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

With Selection.FormatConditions(1).Borders(xlLeft)
    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin
End With

With Selection.FormatConditions(1).Borders(xlRight)
    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin
End With

With Selection.FormatConditions(1).Borders(xlTop)
    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin
End With

With Selection.FormatConditions(1).Borders(xlBottom)
    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin
End With

Selection.FormatConditions(1).StopIfTrue = False

End Sub

舊帖子,但對於未來的訪問者,這里是一個優雅的解決方案,可以為 VBA 中的(非)空白單元格創建條件格式...

如果要將格式條件添加到所選單元格:

' Add new condition to format blank cells. 
' For non-blanks use xlNoBlanksCondition
Selection.FormatConditions.Add Type:=xlBlanksCondition 

' Make it the first conditional format
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

' Apply formatting to condition
' Thin black border all around
With Selection.FormatConditions(1).Borders

    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin    

end With

暫無
暫無

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

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