Consulting

Results 1 to 9 of 9

Thread: VBA date macro

  1. #1

    VBA date macro

    I want to check if a cell has a value of, say, 10:00:00 AM, and then a loop will be entered into.

    [vba]
    Range("J756:J761").Select
    Selection.Copy
    Range("J762").Select
    Do While IsEmpty(ActiveCell.Offset(0, -7)) = False
    ActiveCell.Offset(1, 0).Select

    Do While ActiveCell.Offset(0, 2) = "10:00:00 AM"

    ActiveSheet.Paste
    ActiveCell.Offset(1, 0).Select
    Loop

    Loop
    End Sub
    [/vba]

    The column with dates has cells with format h:mm:ss
    However, I think the way I am referring to the date value is incorrect, because my code doesn't function the way I intended.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    If ActiveCell.Offset(0,2) contains a serial date, it's type Double or Date.
    .
    [VBA]Do While ActiveCell.Offset(0, 2).Value = TimeValue("10:00:00 AM") [/VBA]

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If the cell is formatted in exactly that way, you could test the text property

    [vba]

    Do While ActiveCell.Offset(0, 2).Text = "10:00:00 AM"
    [/vba]
    ____________________________________________
    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

  4. #4
    Thanks!

    Sort of similar situation, now I have this code:
    [vba]If ActiveCell.Offset(0, 1).Value > time Then[/vba]

    time is a Date variable whilst the activecell.offset code refers to cells which are like 9:00:00 AM, 9:00:01 AM, etc. So VBA reads the ActiveCell.Offset(0,1).Value part as numerical eg "0.4666667" etc instead of as time format.

    I tried using ActiveCell.Offset(0,1).Text ......... but this doesn't seem to work with the greater than operator ? Because then the argument looks like: 10:00:00 > 9:00:00 AM (when I hover over the relevant code in the VBA module/coding window)..............but it doesn't enter the IF function so i think it's got something to do with that A.M. part ?

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Time is a VBA date function as well.

    It returns, well, the current time.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That is because Excel stores time as a fraction of one (1 day). So 12:00pm is stored as 0.5, 8:00am is .33333, etc., so comparing the value against time will work okay.
    ____________________________________________
    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 !
    Last edited by wakwak1; 01-12-2008 at 05:13 PM.

  8. #8
    Similar date/time query:

    I have a time variable, initiallized at 9:59:00 AM and I want to increment it by the amount that is present in cells D2,E2, etc. (So D2 could be 1, E2 could be 2, F2 could be 5.......then in the first case it time would be 10:00:00 AM, in the 2nd case it would be 10:01:00 AM etc).

    I tried something like:
    [VBA] For i = 1 to 12
    time = time + "0:" & Cells(2, k)
    k = k + 1
    Next i [/VBA]

    But it gives me a mismatch error, because time is a "date" variable....and the cells(2,k) are also date format....but they have values like "01" , "02", etc.

    Any thoughts?

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    For i = 1 To 12
    time = time + TimeSerial(0, 0, Cells(2, i + 3))
    Next i
    [/vba]
    ____________________________________________
    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

Posting Permissions

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