首页 > 解决方案 > 通过字符串中的前几个字母进行条件格式[VBA]

问题描述

我试图通过应用 .FormatConditions.Add(xlTextString, etc.)到一个范围使一些 VBA 单元格的字体变粗。

如果该范围内的单元格的值以字母“V”开头,我想加粗该范围内的单元格

下面是我正在使用的代码的一部分。我有兴趣从这里获得功能性结果,但我不确定从这里去哪里 - 任何人都可以建议吗?

With .Range("L2:EZ5000").FormatConditions
    .Add(xlTextString,)


End With

标签: excelvbaformatconditional-statements

解决方案


从宏记录器:

Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, _
Formula1:="=LEFT(A1,1)=""V"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
    .Bold = True
    .Italic = False
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False

并整理:

Dim rng As Range, fc As FormatCondition

Set rng = Selection

rng.FormatConditions.Delete
With rng.FormatConditions.Add(Type:=xlExpression, _
         Formula1:="=LEFT(" & rng.Cells(1).Address(False, False) & ",1)=""V""")
    .SetFirstPriority
    .Font.Bold = True
    .StopIfTrue = False
End With

推荐阅读