Excel VBA 中的条件格式使用格式条件中的当前月份和年份值
Conditional formatting in Excel VBA using the current month and year values in the format conditions
在 Excel 2013 年,我试图有条件地格式化代表澳大利亚日期 (dd.mm.yyyy) 的一系列值,并将句点作为分隔符。这些值都采用常规格式。
我已经录制了一个宏来有条件地格式化所有包含特定文本“.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 的新手,正在尝试摸索。
这是我的尝试。你可以试试。
Public Sub test()
Dim Mnow As String
Dim Ynow As String
Dim Finalstring As String
Ynow = Year(Now)
Mnow = Month(Now) + 1
If Mnow < 10 Then
Mnow = "0" & Mnow
Else
Mnow = Mnow
End If
Finalstring = Mnow & "." & Ynow
Sheets("MM All").Select
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlTextString, String:=Finalstring, _
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
End Sub
你可以试试 dateserial
CDate(DateSerial(Year(Now), Month(Now), Day(Now)))
然后根据需要使用 format
进行格式化
Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "yyyy-mm-dd")
Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "yyyy-mmmm-dd")
Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "dd.mm.yy")
还要注意格式函数的语言环境敏感性
或者直接使用
myday = Day(Now)
mymonth = Month(Now)
myyear = Year(Now)
vardate = DateValue(myday & "/" & mymonth & "/" & myyear)
对于那些 VBA 不是必需的,条件格式公式规则可以用于:
=AND(1*MID(A1,FIND(".",A1)+1,2)=MONTH(NOW()),1*RIGHT(A1,4)=YEAR(NOW()))
在 Excel 2013 年,我试图有条件地格式化代表澳大利亚日期 (dd.mm.yyyy) 的一系列值,并将句点作为分隔符。这些值都采用常规格式。
我已经录制了一个宏来有条件地格式化所有包含特定文本“.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 的新手,正在尝试摸索。
这是我的尝试。你可以试试。
Public Sub test()
Dim Mnow As String
Dim Ynow As String
Dim Finalstring As String
Ynow = Year(Now)
Mnow = Month(Now) + 1
If Mnow < 10 Then
Mnow = "0" & Mnow
Else
Mnow = Mnow
End If
Finalstring = Mnow & "." & Ynow
Sheets("MM All").Select
Cells(1, 1).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlTextString, String:=Finalstring, _
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
End Sub
你可以试试 dateserial
CDate(DateSerial(Year(Now), Month(Now), Day(Now)))
然后根据需要使用 format
进行格式化Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "yyyy-mm-dd")
Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "yyyy-mmmm-dd")
Format(CDate(DateSerial(Year(Now), Month(Now), Day(Now))), "dd.mm.yy")
还要注意格式函数的语言环境敏感性
或者直接使用
myday = Day(Now)
mymonth = Month(Now)
myyear = Year(Now)
vardate = DateValue(myday & "/" & mymonth & "/" & myyear)
对于那些 VBA 不是必需的,条件格式公式规则可以用于:
=AND(1*MID(A1,FIND(".",A1)+1,2)=MONTH(NOW()),1*RIGHT(A1,4)=YEAR(NOW()))