简体   繁体   中英

Setting Cell Value Based On Date Excel VBA

When the macro is run on any given date, it should check the current date and compare it to the different quarters in the year, and then change the value of certain cells based on what quarter we're in.

This is what I have so far that I'm trying to append to the end of our current macro:

Sub Dates()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim mgr, rsk As Range
    Dim d, Q1B, Q1E, Q2B, Q2E, Q3B, Q3E, Q4B, Q4E As Date

    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("Mutual")
    Set mgr = ws.Range("B37")
    Set rsk = ws.Range("F37")

    d = Date
    Q1B = DateValue("01/01/2015")
    Q1E = DateValue("03/31/2015")
    Q2B = DateValue("04/01/2015")
    Q2E = DateValue("06/30/2015")
    Q3B = DateValue("07/01/2015")
    Q3E = DateValue("09/30/2015")
    Q4B = DateValue("10/01/2015")
    Q4E = DateValue("12/31/2015")

    If d <= Q1E Then
        mgr.Value = "Works 1"
        rsk.Value = "Works 2"
    End If


End Sub

Am I on the right path? I'm sure there's a more elegant way to write this, but this is my first time writing my own VBA code.

No need for a macro try this formula instead

=IF(COUNTA(A2)=1,"Q"&INT((MONTH(A2)/3.25)+1)&" - "&YEAR(A2),"")

where A2 is your cell containing a date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM