非空白上的Excel 2010 VBA条件格式

时间:2014-10-15 11:36:46

标签: excel-vba access-vba conditional-formatting vba excel

所以我正在制作一个excel工作簿,其中包含来自Access的大量工作表。我想仅在非空白单元格上应用网格线(或边框)。

通过excel手动完成它是微不足道的,但是当我在我的代码上应用它时,宏i记录器似乎不起作用。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

旧帖子,但对于未来的访问者来说,这是在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