• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Help automatically refreshing a year using VBA|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Help automatically refreshing a year using VBA|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosHelp automatically refreshing a yea…
sp_PrintTopic sp_TopicIcon
Help automatically refreshing a year using VBA
Avatar
Janai Zayas

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
April 16, 2020
sp_UserOfflineSmall Offline
1
April 16, 2020 - 1:30 am
sp_Permalink sp_Print

Hi all,

I was hoping to get some help with this problem I'm having. I'm creating a training history spreadsheet for employees. I have the current year and the 2 previous years, so 2018, 2019, and 2020. Using VBA code, I'd like to automatically update these 3 years at the end of each year (when the date is 12/31/2020, 12/31/2021, etc. it updates the 3 years) so that its the current year and 2 previous. For example, 2018, 2019, 2020 would become 2019, 2020, 2021 and so on and so forth when the date of the current year is past 12/31/20XY. I'm trying to do this using if statements but am struggling with making it a loop and getting it to run every time the workbook is opened. I'm wondering if I should use while statements instead? Much of my trouble comes from only wanting to display the year for these columns but trying to compare it with a full date (2018 vs. 12/31/2020) and the fact that the starting years are in the past so I'd have to write some additional code to make up for this and get it on the right track starting 2021.

I'm currently practicing different blurbs of code on a random spread sheet right now to see line by line if its working or not and seeing what I need to adjust but I've run into a wall thought wise. I could just be overthinking it though. Anything helps!

I've attached my code below, I've also tried other ways but this is the most recent one I've been working on.


Sub test()

' This is an Excel VBA code designed to automatically update the workbook
' and display the most recent year and 2 years prior for trainings

' This code will automatically run upon opening the workbook

' This code will update the years after December 31st 20XX, therefore
' for each new year, information from the 3rd previous year
' should be recorded for future use so as not to lose any
' training histories

'------------------------------------------------------------------------

' Set initial year values for overall spreadsheet in first 3 columns
Dim Y1 As Variant
Y1 = 2018
Range("c10").Value = Y1
Range("d10").Value = Y1 + 1
Range("e10").Value = Y1 + 2

' Set current date to display in mm/dd/yyyy format

Dim currentdate As Date
currentdate = Date
Range("h10").Value = currentdate

' Set initial end of year date to display in mm/dd/yyyy format

Dim endofyear As Date
endofyear = CDate("12/31/2020")
Range("i10").Value = endofyear

Dim endofyear2 As Date
endofyear2 = DateSerial(2020, 12, 31)
Range("f15").Value = endofyear2

' This portion of the code is an if statement to determine whether or not
' the current date has past the end of year date
' If it has, the initial year values will all update by 1 or 2 year(s), if
' not they will remain the same

If currentdate < endofyear2 Then
Range("c10").Value = Range("c10").Value
Range("d10").Value = Range("c10").Value + 1
Range("e10").Value = Range("c10").Value + 2
Else:
Range("c10").Value = Range("c10").Value + 1
Range("d10").Value = Range("c10").Value + 1
Range("e10").Value = Range("c10").Value + 2
End If

End Sub

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
April 16, 2020 - 9:00 am
sp_Permalink sp_Print

Hi Janai,

Please attach your workbook so I can see the data.

Regards

Phil

Avatar
Janai Zayas

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
April 16, 2020
sp_UserOfflineSmall Offline
3
May 6, 2020 - 1:53 am
sp_Permalink sp_Print

So sorry for the delayed response, I got pulled into other projects and this was put on the back burner for a bit. 

 

I've attached the file, just deleted the employee last names for privacy reasons. On the overview sheet, I'd like the years for each training to automatically update like I mentioned before. Each employee has their own training history spreadsheet when the link on their name is clicked. These years should also update as they are linked to the years on the overall sheet. 

 

Hope this makes sense, thanks. 

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1518
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
May 6, 2020 - 9:32 am
sp_Permalink sp_Print

Hi Janai,

The code you've supplied doesn't seem to tie in with the data you provided e.g. the code refers to Range("C10"), Range("D10") and Range("E10") but those cells don't contain dates in any of the sheets in your workbook.

I think you might be better off using a pivot table and slicer for what I think you are trying to do.  I've added a new sheet called data and rearranged some data into a form that can be used in a pivot table.  On the Pivot table sheet you'll see a PT that summarises the training for those employees over the years. 

Using the slicer you can choose the years to view.  All you'd need to do is continue adding rows to the data table as employees undertake training, then refresh your pivot table.

Regards

Phil

Avatar
Janai Zayas

Active Member
Members
Level 0
Forum Posts: 3
Member Since:
April 16, 2020
sp_UserOfflineSmall Offline
5
June 2, 2020 - 10:10 pm
sp_Permalink sp_Print sp_EditHistory

Hi Phil, 

The code is referring to the 3 years shown for each training, so the present year and 2 years prior. Since employees began at different times and trainings were held on several dates through any given year, I decided to input the actual date specific to an individual employee in their personal sheet and just put the general year in for the overview sheet. 

I'm not familiar with pivot tables so I would have to learn that method as well. I think we want to limit the amount of manual labor that's involved and have things automatically refresh via code or logic statements upon workbook opening. 

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Tiffany Kang
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 871
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
Jessica Stewart: 205
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Othman AL MUTAIRI
Misael Gutierrez Sr.
Attif Ihsan
Kieran Fee
Murat Hasanoglu
Brett Dryland
Saeed Aldousari
Bhuwan Devkota
Kathryn Patton
Maria Conatser
Forum Stats:
Groups: 3
Forums: 24
Topics: 6222
Posts: 27293

 

Member Stats:
Guest Posters: 49
Members: 31917
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.