Results 1 to 7 of 7
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ingreasing var date + 1 day (2000 sr-1)

    If you have declared MyDate As Date, you can simply use

    MyDate = MyDate + 1

    VBA will automatically take leap years into account.

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ingreasing var date + 1 day (2000 sr-1)

    You need CDate:
    <pre> Dim mydate As Variant
    mydate = CDate("31/12/2006")
    mydate = mydate + 1
    MsgBox (FormatDateTime(mydate, vbShortDate))
    ' Or
    Dim mydate2 As Date
    mydate2 = CDate("31/12/2006")
    mydate2 = mydate2 + 1
    MsgBox (FormatDateTime(mydate2, vbShortDate))
    </pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    increasing var date + 1 day (2000 sr-1)

    Admitting i have this var mydate ="31/12/2006" (i have dim mydate as sdate), ho to increasing that with + 1 day and i want to have mydate "01/01/2007"
    naturally ma ke a controll on 28/02 and 29/02

  4. #4
    Gold Lounger
    Join Date
    Jan 2004
    Location
    Italy
    Posts
    3,245
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ingreasing var date + 1 day (2000 sr-1)

    hummmmm, nice...

    but if i want MyDate + 1 month or MyDate + 1 year, how to?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: ingreasing var date + 1 day (2000 sr-1)

    You can use the DateAdd function for this:

    To add one month: <code>MyDate = DateAdd("m", 1, MyDate)</code>
    To add one year: <code>MyDate = DateAdd("yyyy", 1, MyDate)</code>

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: ingreasing var date + 1 day (2000 sr-1)

    Why declare mydate as a Variant? It is much more efficient to declare mydate as Date. If you declare it as Variant, then VBA has to use extra memory to keep track of what is currently stored in mydate and extra code each time my date is used to determine what data type is currently there. In some cases using a variant is required because different data types can be stored in the variable. However, in this case I don't see any reason for the extra overhead.
    Legare Coleman

  7. #7
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: ingreasing var date + 1 day (2000 sr-1)

    I would never declare it as variant: I just didn't know how sal had defined it, so I provided two sets of code. The second set is preferred with Dim mydate2 as Date
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

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