【发布时间】:2015-06-02 23:21:30
【问题描述】:
在 Excel 2013 中,我尝试有条件地格式化表示澳大利亚日期 (dd.mm.yyyy) 的一系列值,其中句点作为分隔符。这些值都被格式化为General。
我已经录制了一个宏来有条件地格式化包含特定文本“.04.2015”的所有值,但在 vba 中它有 Selection.FormatConditions.Add Type:=xlTextString, String:="03.2015",我想让它使用当月的值 Month (Now)' 和当年Year(Now)。
记录代码:
Sheets("MM All").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlTextString, String:="03.2015", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.Color = -16711681
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
我正在尝试什么(我刚刚包含了上面的相关代码)
Sheets("MM All").Select
Dim MNow As String
Dim NMth As String
Dim YNow As String
MNow = Month(Now)
NMth = Month(Now) + 1
YNow = Year(Now)
Range("E2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
If (Unsure how to write) Mnow value is single digit
Selection.FormatConditions.Add Type:=xlTextString, String:="0" & "(MNow)" & "." & "(YNow)",
当 Mnow 和 Nmth 值是两位数时,我还会包含 IF,我只是不会在字符串中连接额外的 0。
任何有关如何使用月(现在)和年(现在)的值并将它们连接到格式条件字符串的帮助将不胜感激。我是 VBA 的新手,并试图感受我的方式。
【问题讨论】:
标签: vba date excel conditional-formatting