dimanche 15 août 2021

Microsoft VBA - How to run different code depending on the current date in relation to set date ranges

Basically I'm trying to run 4 sets of code, each set depending on what quarter of the year it is i.e. if the current date is between 1/1 - 3/31 then run xx code, if the current date is between 4/1 - 6/30 then run yy code and so on etc.

Is there a way to set that up within the actual code and not using Excel? As you can probably tell, I'm not an expert by any means and have picked up most of what I know from trial and error and browsing here/google. I've searched around quite a bit but can't find this specific topic so any help is appreciated.

Thanks!

    Sub QuarterlyCheck ()

    DateToday = Date$
  
    Dim EditedDate As String
    EditedDate = DateAdd("d", 0, DateToday)
    Dim EditedDate2 As String
    EditedDate2 = Replace(EditedDate, "/", "-")
    EditedDate3 = Format(EditedDate2, "mm-dd-yyyy")

    If EditedDate3 is >= 1/1/2021 and <= 3/31/2021 then
        Do this...

    Else If EditedDate3 is >= 4/1/2021 and <= 6/30/2021 then
        Do this...

    Else If EditedDate3 is >= 7/1/2021 and <= 9/30/2021 then
        Do this...

    Else If EditedDate3 is >= 10/1/2021 and <= 12/31/2021 then
        Do this...

    EndIf

    End Sub

Aucun commentaire:

Enregistrer un commentaire