Consulting

Results 1 to 17 of 17

Thread: Solved: Pls explain time limit in code

  1. #1
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location

    Solved: Pls explain time limit in code

    I need an explanation on the time limit in this code.
    The way I read it (probably wrong) I interpret it to be like this.
    If the date is 14 days past starting date (19-06-2006) then show the promt.
    Question : Why then today when I run the code do I not get to see the prompt ?
    Many thanks

    [vba]Private Sub Workbook_Open()
    Dim incRange As Range
    Dim StartDate As Date

    Set incRange = Range("Increment")
    StartDate = "19/06/06" 'Date delivered to customer

    Sheets("hidden").Visible = xlVeryHidden
    incRange = incRange + 1
    Me.Save
    If incRange > 10 Or Date - StartDate > 14 Then
    Application.DisplayAlerts = False
    MsgBox "over the use limit, contact owner"
    Me.Close
    End If

    End Sub[/vba]
    Thank you for your help

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't know, it works fine for me.

    Do you have a sheet named 'hidden'?

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    StartDate is a string not a date as far as I can see.

    Perhaps that's causing a problem.

  4. #4
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    to xld: yes I have the sheet "hidden" it increments the times the files is opened but still works with the date (string) as shown in the code.
    To Norie : how can I then replace it with a date to test it and see if it works ?

    Also is my interpretation of the code correct ?
    Thank you for your help

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm seeing the message box. Have you tried stepping throught code and using the Watch window to check the values?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Better still here is the file

    Sorry Malcom .. was typing while you where posting ... I will try
    Thank you for your help

  7. #7
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Tried and all appears normal ... the prompt shows on the 11th time the user opens the file ... but with the start date in there and again the way I understand it I should see the promt from the very start no? 19/06/06 = June 19th 2006 + 14 days = July 03 2006 ... we are Nov 10th 2006 ... so I must be interpreting the meaning in the code wrongly.
    Thank you for your help

  8. #8
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    I think I've got it now ... must be that the original code was posted from Europe ... I've changed it to 06/16/06 and it works. If I'm wrong please bring it to my attention ... I will mark it solved but will check just in case
    Thank you for your help

  9. #9
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    The code works for me too.

    Where are you located?

    Perhaps it's an issue with your date settings.

    In Europe what might be happening is VBA is recognizing the string "19/06/06" as a valid date and therefore converting it to a 'true' date value.

    Where the date format is different it might not be doing that.

    I would suggest you take a look at the DateValue or DateSerial functions to set the date.

  10. #10
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Hello Norie and thank you for the followup.
    I'm in Canada ... I just changed 19/06/06 to 10/20/2006 and it works.
    If you have the time and would like to explain in details what you mean you will
    do me a great favour as the file might be sent to Europeans as well.
    Many thanks
    Nick
    Thank you for your help

  11. #11
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Nick

    Using DateSerial you could set the date like this.
    StartDate = DateSerial(2006, 10, 20)
    Which would be 20th November 2006.

  12. #12
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location

    dates ...

    Quote Originally Posted by ndendrinos
    I need an explanation on the time limit in this code.
    The way I read it (probably wrong) I interpret it to be like this.
    If the date is 14 days past starting date (19-06-2006) then show the promt.
    Question : Why then today when I run the code do I not get to see the prompt ?
    Many thanks

    [vba]Private Sub Workbook_Open()
    Dim incRange As Range
    Dim StartDate As Date

    Set incRange = Range("Increment")
    StartDate = "19/06/06" 'Date delivered to customer

    Sheets("hidden").Visible = xlVeryHidden
    incRange = incRange + 1
    Me.Save
    If incRange > 10 Or Date - StartDate > 14 Then
    Application.DisplayAlerts = False
    MsgBox "over the use limit, contact owner"
    Me.Close
    End If

    End Sub[/vba]
    when you want 19th july use #06/19/2006#. That will even work in Europe (even in Belgium).

    Charlize

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Tricky things dates!
    Quote Originally Posted by Norie
    Code:
    StartDate = DateSerial(2006, 10, 20)
    Which would be 20th November 2006.
    and
    Quote Originally Posted by Charlize
    when you want 19th july use #06/19/2006#.

    Charlize
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Indeed, maybe time to sleep ...

  15. #15
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Norie, Charlize is Malcom having fun at your expense?
    Do I understand that both of you were distracted and that :
    [VBA]
    StartDate = DateSerial(2006, 10, 20)
    Which would be 20th November 2006.[/VBA]
    S/B 2006,11,20

    and
    [VBA]when you want 19th july use #06/19/2006#.[/VBA]
    S/B #07/19/2006#

    Or am I misssing something here ?
    Thank you for your help

  16. #16
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location

    the Nail

    You the nail.

    Charlize

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by ndendrinos
    Norie, Charlize is Malcom having fun at your expense?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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