Consulting

Results 1 to 8 of 8

Thread: Help automatically refreshing a year using VBA

  1. #1
    VBAX Newbie
    Joined
    Apr 2020
    Posts
    4
    Location

    Help automatically refreshing a year using VBA

    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
    Last edited by SamT; 04-15-2020 at 04:34 PM.

  2. #2
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    To run a routine when the workbook opens use the Workbook_Open procedure in ThisWorkbook module eg:

    VBAX67149a.jpg

    What are you wanting to copy, from where and to where?
    Semper in excretia sumus; solum profundum variat.

  3. #3
    VBAX Newbie
    Joined
    Apr 2020
    Posts
    4
    Location
    I'm not trying to copy anything really, I'm mainly trying to get it to update the years I'm starting with (2018,2019,2020) each year by comparing it to the end of year date (12/31/20XY) so when its the new year (1/1/20XY) the column years have no increased by 1 (2019,2020,2021) now

  4. #4
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    This should do it:

    Sub Wow()
        Dim yr As Long, x As Long
        yr = Year(Now) - 2
        For x = 3 To 5
            Cells(10, x) = yr
            yr = yr + 1
        Next
    End Sub
    Semper in excretia sumus; solum profundum variat.

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I'd like to automatically update these 3 years at the end of each year
    Private Sub Workbook_Open()
    If CDate(CStr(Format(Date, "yyyy"))) = CDate(Range("E10") + 1) Then
        Range("c10") = Range("c10") + 1
        Range("d10") = Range("d10") + 1
        Range("e10") = Range("e10") + 1
    End If
    End Sub
    Alternate IF ... Then
    If Date > Range("I10" ) Then
    IF Your code
    ' 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
    was
    ' Set initial year values for overall spreadsheet in first 3 columns
    Dim Y1 As Variant
        Y1 = 2018
        Range("c10").Value = Y1
        Range("d10").Formula = "= Range("c10") + 1"
        Range("e10").Formula = "= Range("c10") + 2"
    Then
    Private Sub Workbook_Open()
    If Date > Range("I10" ) Then Range("c10") = Range("c10") + 1
    End Sub

    Note that "+1" will only increase a number like 2020 by one.
    To increase a Date year like 2020 by one, use
    DateAdd(1, "yyyy", Range("E10"))
    Using +1 with a Date like 2020 will result in a Date of 1/1/2020


    EndOfYEar of Any date
    Public Function EOY(AnyDate As Variant) As Date
       EOY = CDate("12/31/" & Year(AnyDate))
    End Function
    Last edited by SamT; 04-15-2020 at 05:38 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Newbie
    Joined
    Apr 2020
    Posts
    4
    Location
    Sorry I'm getting back to you all so late, I got introduced a completely different project and had to put this one on the back burner. May I ask what the "x" represents in this? and why the "for x = 3 to 5"? I understand this is a for loop and it's meant to add on each additional year but I'm stuck on that part of code. I'm not the best with coding, please forgive me!

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Step through the code (F8) with the Locals window open to see the variables in real time.

    Sub Wow()
        'Declare variables: yr represents the year, x is a variable in the For-Next loop
        Dim yr As Long, x As Long
        'yr = the current year minus two (2018 in 2020!)
        yr = Year(Now) - 2
        'The variable x increases by 1 on every loop.  It starts at 3.
        For x = 3 To 5
            '1st time round the loop x=3 so Cells(Row number, Column number) which is range C10 will = 2018
            '2nd time round x=4 so Cells(10, 4) which is range D10 will = 2019 (don't forget we added 1 to the yr)
            '3rd time round x=5 so Cells(10, 5) which is range E10 will = 2020
            Cells(10, x) = yr
            'Add 1 to the year each time round the loop
            yr = yr + 1
        'If x is between 3 and 5 repeat the loop
        Next
        'x = 5 finished
    End Sub
    
    'This is the same in long hand
    
    Sub Wowzie()
        Dim yr As Long, x As Long
        yr = Year(Now)
        yr = yr - 2
        x = 3
        Cells(10, x) = yr
        yr = yr + 1
        x = x + 1
        Cells(10, x) = yr
        yr = yr + 1
        x = x + 1
        Cells(10, x) = yr
    End Sub
    Semper in excretia sumus; solum profundum variat.

  8. #8
    VBAX Newbie
    Joined
    Apr 2020
    Posts
    4
    Location
    Thank you so much! It works!!! Is it possible to shift cell values to the left based on a condition?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •