Consulting

Results 1 to 11 of 11

Thread: Working with "time"

  1. #1

    Working with "time"

    Hello everyone,

    I am having a probably very easy problem here. I am trying to find out in what format VBA sets time and how I can work with times. I just want an easy script that tells the user "good morning", "good afternoon" and "good night".
    This is what i have


    Dim Dag As String
    Dim Tijd
    Tijd = Time
    If Tijd <= "12:00:00" Then Dag = "Good Morning"
    If "12:00:00" < Tijd <= "18:00:00" Then Dag = "Good Afternoon"
    If Tijd > "18:00:00" Then Dag = "Good Night"

    When I have it like this it tells me all kind of weird stuff, so good night in the morning, etc. But I can't seem to find out how VBA handles times.

    TIA
    frank

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Frank,
    Try the following

    Sub Greeting()
    TimePart = Now() - Int(Now())
        Select Case TimePart
            Case Is < 0.5
                Dag = "Good Morning"
            Case Is < 0.75
                Dag = "Good Afternoon"
            Case Else
                Dag = "Good Night"
        End Select
    End Sub
    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'

  3. #3
    Thanks a lot, seems to work, although I have to wait till the afternoon to see if it works because I dont have sufficient rights to change the system time :S

  4. #4
    I have another quick question, do you know how I can get . marks in numbers so when I ask VBA to get a certain value from a cell (for instance 3158392) how I can display it as 3.158.392, I know I can work my way around it with contatenate and right-function, but is there an easier function for this?

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Regouin
    I have another quick question, do you know how I can get . marks in numbers so when I ask VBA to get a certain value from a cell (for instance 3158392) how I can display it as 3.158.392, I know I can work my way around it with contatenate and right-function, but is there an easier function for this?
    Assuming in cell A1, try

    Format(Range("A1").Value,"#\.##0.00")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [QUOTE=mdmackillop]

    TimePart = Now() - Int(Now())
    /QUOTE]

    You can use Time to avoid having to strip the days from the Now function.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Thanks for the help but now I think you edit the format of the cell itself and I just want VBA to display the numbers with 1000 marks.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Regouin
    Thanks for the help but now I think you edit the format of the cell itself and I just want VBA to display the numbers with 1000 marks.
    All I gave you was the syntax, I didn't suggest how to deply it. It will format the cell if yo do that

    Range("A1").Value = Format(Range("A1").Value,"#\.##0.00")
    or you can just MsgBox it

    MsgBox Format(Range("A1").Value,"#\.##0.00")
    or whatever you want.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Yup, got it now.

    Only changed it a bit because I dont need any decimals


    MsgBox Format(Range("A1").Value,"#\.##0")

    Now it works perfect

    thanks

    Frank

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Regouin
    Only changed it a bit because I dont need any decimals
    Frank, from your OP, you probably need


    MsgBox Format(Range("A1").Value,"#\.###\.##0")
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Since it is now afternoon here, i can tell you that it works perfectly. Thanks again

    Frank

Posting Permissions

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