簡體   English   中英

在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。

我將如何使用Month(Now)和Year(Now)中的值並將它們連接到Format Conditions字符串中,這方面的任何幫助將不勝感激。 我是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

您可以嘗試約會

CDate(DateSerial(Year(Now), Month(Now), Day(Now)))

然后根據需要格式化

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()))

暫無
暫無

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

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