VBA Conditional Calling Based on Current Date

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hello,

I have a few different functions that are set up to do different things depending on the current date.

List of functions:
2020_Tasks
2021_Tasks
2022_Tasks


Is there a way, using VBA, to only call the '2020_Tasks' function if the current year is 2020, and then to continue moving through the rest of the sub.
(similarly only calling '2021_Tasks' if the current year is 2021, etc...)

Thanks in advance
 

Some videos you may like

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,235
Office Version
  1. 365
Platform
  1. Windows
What are the actual names of the functions/subs?
 

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Here's an idea of what I'm lookng to do....i know that the syntax is not correct, but hopefully it gives an idea
I realized that I had the function names backwards in my original post. Apologies.

VBA Code:
Sub UpdateData()

' Some sort of conditional statement that establishes this year
    If now = 2020 Then
         Call Tasks_2020  'only call this function if it is being called during the year 2020
    Else If now = 2021 Then
         Call Tasks_2021  'only call this function if it is being called during the year 2021
    Else
         Call Tasks_2022  'only call this function if it is being called during the year 2022
    End If

End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,384
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
   Select Case Year(Date)
      Case 2020
         call 2020_Tasks
      Case 2021
         call 2021_tasks
      Case 2022
         call 2022_Tasks
   End Select
 

default_name

Board Regular
Joined
May 16, 2018
Messages
151
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
How about
VBA Code:
   Select Case Year(Date)
      Case 2020
         call 2020_Tasks
      Case 2021
         call 2021_tasks
      Case 2022
         call 2022_Tasks
   End Select
Wow that was quick.
Does the Year(Date) require any additional coding (defining the current date, year, etc)? Is it really that simple?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,384
Office Version
  1. 365
Platform
  1. Windows
Nothing else needed, date returns the current date & Year then returns the year for that date.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,504
Messages
5,602,057
Members
414,498
Latest member
jordanmiller7890

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top