+ Reply to Thread
Results 1 to 14 of 14

VBA Current Month Minus 1

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Current Month Minus 1

    Hi, I wonder whether someone may be able to help me please.

    I'm using the snippet of code below to, amongst cell formatting, insert the current month to a spreadsheet cell in the format 'mmm yy'.

    With Range("B3")
                    .Value = Format(Date - 1, "1 mmm yy")
                    .NumberFormat = "mmm yy"
                    .HorizontalAlignment = xlCenter
                    .Interior.ColorIndex = 37
                        With .Font
                            .Name = "Lucida Sans"
                            .Bold = True
                            .Size = 10
                        End With
                End With
    What I'd like to do is chage this so the month which is inserted is the 'Current Month -'.

    I've tried changing this line:
    .Value = Format(Date, "1 mmm yy")
    to
    .Value = Format(Date - 1, "1 mmm yy")
    but unfortunately this doesn't work.

    I just wondered whether someone may be able to look at thsi please and let me know where I'm going wrong.

    Many thanks and kind regards

  2. #2
    Registered User
    Join Date
    09-17-2013
    Location
    East
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: VBA Current Month Minus 1

    Check out http://www.techonthenet.com/excel/formulas/dateadd.php

    Hope that helps?

  3. #3
    Registered User
    Join Date
    09-17-2013
    Location
    East
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: VBA Current Month Minus 1

    Quote Originally Posted by Klaster View Post
    Is this working?

    .Value = Format(DateAdd(m, -1, Date), "1 mmm yy")

  4. #4
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @Klaster, thank you very much for coming back to me with this.

    I did try the line yo highlighted, but unfortuantely I recieve the following message: 'Run-time error 5: Invalid procedure call or argument'. Debug highlights this line as the cause
    .Value = Format(DateAdd(m, -1, Date), "1 mmm yy")
    Many thanks and kind regards

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @Klaster, thank you very much for taking the time to reply to my post and for including the link. This very interesting reading, particulalry when, like I, you're learning.

    Kind Regards

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA Current Month Minus 1

    I am not sure if this what you are after

    You can use

      .Value = Date - 1
    Instead of
     .Value = Now - 1

    Sub test()
    
    With Range("B3")
    
                    .Value = Now - 1
                    .NumberFormat = " dd-mmm-yy"
                    .HorizontalAlignment = xlCenter
                    .Interior.ColorIndex = 37
                        With .Font
                            .Name = "Lucida Sans"
                            .Bold = True
                            .Size = 10
                        End With
                End With
    End Sub

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @AB33, thank you very much for taking the time to reply to my post and for putting the code together.

    Unfortunately, although I didn't recieve an error message, the cell stills shows the current month.

    Many thanks and kind regards

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Current Month Minus 1

    if you mean current month I'm not sure how -1 fits in? perhaps
    With Range("B3")
                    .Value = date - day(date) + 1
                    .NumberFormat = "mmm yy"
                    .HorizontalAlignment = xlCenter
                    .Interior.ColorIndex = 37
                        With .Font
                            .Name = "Lucida Sans"
                            .Bold = True
                            .Size = 10
                        End With
                End With
    if you mean last month perhaps
    .Value = Worksheetfunction.eomonth(date, -2) + 1
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  9. #9
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @JosephP, thank you very much for taking the time to reply to my post and for putting the script together.

    I tried your second suggestion, as this fits in with my requirements.

    Unfortunately when I run this I receive the following message Run-time error 438: Object doesn't support this property or metyhod'. Debug highlights this line as the issue:
    .Value = WorksheetFunction.eomonth(Date, -2) + 1
    Many thanks and kind regards

  10. #10
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: VBA Current Month Minus 1

    As JosephP showed, for the first date of the previous month I'd also use the eomonth function and then define the numberformat as "d mmm yy".

    With Range("B3")
        .Value = Evaluate("EoMonth(Today(), -2) + 1")
        .NumberFormat = "d mmm yy"
        .HorizontalAlignment = xlCenter
        .Interior.ColorIndex = 37
        With .Font
            .Name = "Lucida Sans"
            .Bold = True
            .Size = 10
        End With
    End With

  11. #11
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @berlan, thank you very much for taking the time to reply to my post and for putting the solution together, which, with a very small adjustment to the date format works great.

    All the best and kind regards

  12. #12
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: VBA Current Month Minus 1

    Why not try dateserial?

                With Range("B3")
                    .Value = DateSerial(Year(Now), Month(Now) - 1, 1)
                    .NumberFormat = "mmm yy"
                    .HorizontalAlignment = xlCenter
                    .Interior.ColorIndex = 37
                        With .Font
                            .Name = "Lucida Sans"
                            .Bold = True
                            .Size = 10
                        End With
                End With

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Current Month Minus 1

    I guess you aren't really using 2013 then?

  14. #14
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Current Month Minus 1

    Hi @JosephP, my apologies for not beiong clear. I'm using 2013 at home, but 2003 at work.

    Once again my apologies for the confusion.

    Kind regards

+ 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. Define Variable as Current Date minus 7
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2013, 07:25 PM
  2. Macro using vlookups comparing 2 months paysheet(previous month and current month)
    By srinivasan1965 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-07-2012, 03:45 AM
  3. [SOLVED] Auto populate cells from data in a 6 month range starting with the current month
    By ecarnley349 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-21-2012, 07:32 PM
  4. [SOLVED] Auto insert current month's name and current year
    By Webtekr in forum Word Formatting & General
    Replies: 0
    Last Post: 03-03-2009, 05:20 AM
  5. Replies: 11
    Last Post: 06-22-2005, 10:05 AM

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