【问题标题】:Conditional formatting in Excel VBA using the current month and year values in the format conditionsExcel VBA 中的条件格式使用格式条件中的当前月份和年份值
【发布时间】: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


    【解决方案1】:

    这是我的尝试。你可以试试。

    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
    

    【讨论】:

      【解决方案2】:

      你可以试试dateserial

      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)
      

      【讨论】:

        【解决方案3】:

        对于那些不需要 VBA 的人,条件格式公式规则可以服务于:

        =AND(1*MID(A1,FIND(".",A1)+1,2)=MONTH(NOW()),1*RIGHT(A1,4)=YEAR(NOW()))
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-25
          • 1970-01-01
          • 1970-01-01
          • 2011-10-02
          • 2015-07-24
          • 1970-01-01
          相关资源
          最近更新 更多