+ Reply to Thread
Results 1 to 7 of 7

Thread: Macro - subtracting times

  1. #1
    Registered User
    Join Date
    08-16-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Macro - subtracting times

    Hello. I guess this question has been asked many times before but I have done so many searches to try to work this out:

    Using a macro, I wish to take a date/time from within a cell, subtract or add hours and return it as a time (usually to convert between AP/US/BST) time zones. I will assign the macro to a button.
    The date/time value is general format: "08/08/2011 14:17:15"

    Using the worksheet function "=A4 - TIME(5,,)" (for example) returns "9:17AM", which is perfect and exactly what I wanted but i cannot figure out how to produce the same result within a macro.

    I have tried using...
    Range("A4").Select
        Do While IsEmpty(ActiveCell.Offset(0, 1)) = False
        ActiveCell.Value = ActiveCell.Value - <a number of time functions>
        ActiveCell.Offset(1, 0).Select
        Loop
    ... but I must be using the wrong syntax or perhaps I need to format cells first.

    Could anyone guide me to correct the code above to subtract 5 hours from the time in the ActiveCell being processed please?

    Regards,
    Dobbs.
    Last edited by dobbinuk; 08-17-2011 at 08:48 AM.

  2. #2
    Registered User
    Join Date
    08-16-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro - subtracting times

    I have uploaded a sample spreadsheet, which hopefully explains what I want to achieve. Any help gratefully received.

    Regards,
    Dobbs.
    Attached Files Attached Files

  3. #3
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,230

    Re: Macro - subtracting times

    Welcome to the forum.

    Please take a few moments to read the forum rules and add CODE tags to your code sample.

    Regards

  4. #4
    Valued Forum Contributor
    Join Date
    07-20-2011
    Location
    mysore
    MS-Off Ver
    Excel 2003
    Posts
    421

    Re: Macro - subtracting times

    Pl use thefollowing code.Assumed a4 contains date & time.
    m1 subtracts two days from A4 value.
    m2 subtracts 1 Hr 2Mts and 3 Secs from A4 value.
    n gives the required format.

    Sub datetime()
    Dim k As Date
    k = Range("a4").Value
    m1 = ActiveCell.Value - 2
    m2 = ActiveCell.Value - TimeSerial(1, 2, 3)
    
    n = Format(m1, "mm/dd/yyyy hh:mm:ss")
    End Sub

  5. #5
    Extremely Helpful member
    Join Date
    12-23-2006
    Location
    Belgium
    MS-Off Ver
    XL2003
    Posts
    6,127

    Re: Macro - subtracting times

    Kvsrinivasamurthy

    Your post does not comply with Rule 7 of our Forum RULES. Please do not ignore Moderators' or Administrators' requests - note that this includes requests by senior members as well, if you are unclear about their request or instruction then send a private message to them asking for help. Do not post a reply to a thread where a moderator has requested an action that has not been complied with e.g Title change or Code tags...etc
    Quoting entire posts clutters the forum and makes threads hard to read !

    If you are pleased with a member's answer then use the Star icon to rate it

    Click here to see forum rules

  6. #6
    Registered User
    Join Date
    08-16-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro - subtracting times

    Added code tags as requested and also example sheet.

  7. #7
    Registered User
    Join Date
    08-16-2011
    Location
    UK
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Macro - subtracting times

    kvsrinivasamurthy, Thanks for the response. Unfortunately I get the error message "Run-time error '13': Type mismatch when I try to run the code that you supplied.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0