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...... but I must be using the wrong syntax or perhaps I need to format cells first.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
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.
I have uploaded a sample spreadsheet, which hopefully explains what I want to achieve. Any help gratefully received.
Regards,
Dobbs.
Welcome to the forum.
Please take a few moments to read the forum rules and add CODE tags to your code sample.
Regards
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
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
Added code tags as requested and also example sheet.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks