VBA Conditional Calling Based on Current Date

default_name

Board Regular
Joined
May 16, 2018
Messages
180
Office Version
  1. 365
  2. 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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What are the actual names of the functions/subs?
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0
Nothing else needed, date returns the current date & Year then returns the year for that date.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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
Back
Top