+ Reply to Thread
Results 1 to 3 of 3

[SOLVED] VBA Insert Current Month, Year and subsequent months/year for 15 months

  1. #1
    Registered User
    Join Date
    07-19-2014
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4

    [SOLVED] VBA Insert Current Month, Year and subsequent months/year for 15 months

    Second thread ever - sorry if I make mistakes. I'm new to VBA.

    I'm making a macro that finds out the current month & year based off the Now function. I place the month in cell H1, the year in H2, and Month-Year in H3.
    I want all 3 of those cells to show the next month, year, month-year for 15 months in the cells to their right. (I plan to enter a formula in cell H4:V4 that uses COUNTIFS and I plan to use the months and years listed in H1:V1, H2:V2 as the criteria) (Also, I plan to use H3:V3 as the x axis of a graph later on...)
    I know I can quickly do this in excel without a macro, but I'm making the macro for some colleagues who don't know how to use excel and I want to quickly automate a task that is required monthly.


    My attempt: To do this, I used the record macro to select H3 and auto fill until V3 - that worked great for Month-Year...
    However, I tried to take the values of H1 and just add 1 and so on through the cells on the right. My problem arises quickly because the next "month" in cell I1 is 13 instead of 1. I don't know how to subtract 12 if the value is greater than 12. And I have no idea how to make the Year in I2 only add 1 when its a new year.

    I think I need to do an IF THEN statement but I'm not sure how to tackle that.

    Please Login or Register  to view this content.
    As you can see, I have no idea what I'm doing but I've spent a lot of time trying to figure this out.

    Thanks!
    Last edited by MichiganWilliams; 12-19-2014 at 03:48 PM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA Insert Current Month, Year and subsequent months/year for 15 months

    Hi Michigan,

    Nice first attempt. Try the following. I added a little extra code so it would be a little bit easier for you to follow.
    Please Login or Register  to view this content.
    To prevent typos from ruining days and weeks of work 'Option Explicit' is NEEDED at the top of each code module. This prevents errors caused by missspellings and FORCES every variable to be DECLARED (e.g. dim i as Integer). http://www.cpearson.com/excel/DeclaringVariables.aspx

    Lewis

  3. #3
    Registered User
    Join Date
    07-19-2014
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4

    Re: VBA Insert Current Month, Year and subsequent months/year for 15 months

    Thanks so much for your help! It's going to take me awhile to figure out how this works but it works great! Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 09-04-2013, 10:49 AM
  2. Replies: 2
    Last Post: 06-21-2012, 08:17 PM
  3. Getting current & completed months of the year
    By jmoses99 in forum Excel General
    Replies: 1
    Last Post: 03-03-2010, 08:24 AM
  4. Replies: 3
    Last Post: 03-12-2009, 09:54 AM
  5. [SOLVED] HOW TO SUBTRACT A YEAR+MONTH AND SHOW DIF IN MONTHS?
    By noles fan in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-01-2005, 01:05 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1