MrExcel Message Board
With Macromedia Shockwave Player, you can enjoy multimedia games, learning applications, and product demonstrations on the Web, using exciting new 3D technology. Get it at: http://www.macromedia.com/shockwave/download


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Jan 20th, 2004, 08:12 PM   #1
BCS2
 
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
Default Format Times in AM/PM

Is there a way to format a cell to automatically drop in the AM/PM designation based on the time entered?

For example, our hours of business are 8 AM - 5 PM. I would like to have all times between 8:00 and 11:59 designated as AM and all times between 12:00 and 5:00 as PM.

Thanks All

BCS2 is offline   Reply With Quote
Old Jan 20th, 2004, 08:21 PM   #2
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
Default Re: Format Times in AM/PM

Quickest way I can think of is to drop a bit of code on the Worksheet's Change Event handler. This example assumes times are being entered in column A. Just change the target column to suit your needs.

Private Sub Worksheet_Change(ByVal Target As Range)
****If Target.Count <> 1 _
****Or Target.Column <> 1 Then Exit Sub
****If Target < TimeSerial(8, 0, 0) Then Target = Target + 0.5
End Sub


[Edit]I interpreted your request to be that you were hand-keying in times and didn't want to have to key in the PM bit (or use military time) for each entry. Apologize if I misunderstood. [EndEdit]
__________________
Greg
………………………………………………
Work: XL2003/2007 on WinXP
Home: 02, 03 & 07 on Vista
Microsoft MVP - Excel (proof that truth is stranger than fiction)
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Old Jan 20th, 2004, 08:21 PM   #3
Ekim
 
Join Date: Jul 2002
Location: Perth, Australia
Posts: 1,416
Default Re: Format Times in AM/PM

Select your cell(s)
Format menu | Cells | Number tab | Custom
Scroll down until you find: “h:mm AM/PM” (this a standard custom format)
OK.

If you can’t find it, then create it. Do step 2 above, then in the box headed “Type”, enter
“h:mm AM/PM” (no quotes)
OK

Regards,

Mike
Ekim is offline   Reply With Quote
Old Jan 20th, 2004, 08:41 PM   #4
BCS2
 
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
Default Re: Format Times in AM/PM

Quote:
Originally Posted by Greg Truby
Quickest way I can think of is to drop a bit of code on the Worksheet's Change Event handler. This example assumes times are being entered in column A. Just change the target column to suit your needs.

[Edit]I interpreted your request to be that you were hand-keying in times and didn't want to have to key in the PM bit (or use military time) for each entry. Apologize if I misunderstood. [EndEdit]
You interpreted correctly, but how do I access the worksheet's change event handler? I also don't see where you have specified column A in your code.

Thanks
BCS2 is offline   Reply With Quote
Old Jan 20th, 2004, 09:03 PM   #5
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
Default Re: Format Times in AM/PM

Easiest way is to right-click the tab at the bottom of the worksheet in question. You should get a short popup menu. On that menu should be an option for View Code (normally it's the last option). Click on View Code and then just paste in the code above.

Column "A" is the same as Column 1. So the bit that reads: Or Target.Column <> 1 is what is checking for column A. If your times are in column B, change the "1" to a "2", if in column Z, change the "1" to "26" and so forth. Also, you may want to avoid processing header row(s). You can do this by adding a condition that check Target.Row. For example, the following is how the IF statement would look if Times are in Column G, and you have two (2) header rows:

Code:
If Target.Count <> 1 _
Or Target.Row <= 2 _
Or Target.Column <> 7 Then Exit Sub
Regards,
__________________
Greg
………………………………………………
Work: XL2003/2007 on WinXP
Home: 02, 03 & 07 on Vista
Microsoft MVP - Excel (proof that truth is stranger than fiction)
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Old Jan 20th, 2004, 09:38 PM   #6
BCS2
 
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
Default Re: Format Times in AM/PM

What if the times are being entered into multiple columns?

Does this require separate code for each column or can I designate multiple columns in the same subroutine?
BCS2 is offline   Reply With Quote
Old Jan 20th, 2004, 09:51 PM   #7
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
Default Re: Format Times in AM/PM

On routine, but for better clarity in reading the code, probably better to split the IF into two pieces:

Private Sub Worksheet_Change(ByVal Target As Range)
****If Target.Count <> 1 _
****Or Target.Row <= 2 Then Exit Sub
****
****If Intersect(Target, Union([B:C], [F:F], [I:M])) Is Nothing Then Exit Sub
****
****If Target < TimeSerial(8, 0, 0) Then Target = Target + 0.5

End Sub



The above would only execute on columns B, C, F, I, J, K, L, and M.
__________________
Greg
………………………………………………
Work: XL2003/2007 on WinXP
Home: 02, 03 & 07 on Vista
Microsoft MVP - Excel (proof that truth is stranger than fiction)
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Old Jan 20th, 2004, 10:22 PM   #8
BCS2
 
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
Default Re: Format Times in AM/PM

Here is the code I have so far and a sample of my data. It' giving me a "Compile error: Argument not optional" Something to do with the Union command.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count <> 1 _
Or Target.Row <= 2 Then Exit Sub

If Intersect(Target, Union([D:H])) Is Nothing Then Exit Sub

If Target < TimeSerial(8, 0, 0) Then Target = Target + 0.5

End Sub

By the way, I have 2 header rows on my Excel sheet, I just didn't get copy that row to my sample data.

******** ******************** ************************************************************************>
Microsoft Excel - Estimate tracking.xls___Running: xl2000 : OS = Windows 98
(F)ile (E)dit (V)iew (I)nsert (O)ptions (T)ools (D)ata (W)indow (H)elp (A)bout
=

D
E
F
G
H
2
E-mail*date*/*timePrint*out*date*/*timeDue*date*/*timeStart*date*/*timeDate*/*Time*delivered
3
1/6/04*9:42*AM1/6/04*9:45*AM1/6/04*5:00*PM*1/6/04*10:49*AM
4
1/6/04*9:41*AM1/6/04*9:45*AM1/6/04*5:00*PM*1/6/04*12:00*PM
5
1/6/04*1:38*PM1/6/04*1:49*AM1/7/04*12:00*PM*1/6/04*2:40*PM
Estimate Tracking*

[HtmlMaker 2.42] To see the formula in the cells just click on the cells hyperlink or click the Name box
PLEASE DO NOT QUOTE THIS TABLE IMAGE ON SAME PAGE! OTHEWISE, ERROR OF JavaScript OCCUR.


Again, I am looking for code that will automatically assign an AM/PM designation a cell based on the time entered.

Thanks for your patience.
BCS2 is offline   Reply With Quote
Old Jan 20th, 2004, 10:42 PM   #9
Greg Truby
MrExcel MVP
Int'l Moderator
 
Greg Truby's Avatar
 
Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
Default Re: Format Times in AM/PM

No problem. The Union is failing because it wants more than one range. If your columns are contiguous, as they are here, you can forego it. Also, you are typing in dates and times? If yes, then the code needs a slight tweak (see below).

Private Sub Worksheet_Change(ByVal Target As Range)
****If Target.Count <> 1 _
****Or Target.Row <= 2 Then Exit Sub
****
****If Intersect(Target, [D:H]) Is Nothing Then Exit Sub
****
****If (Target - Int(Target)) < TimeSerial(8, 0, 0) Then Target = Target + 0.5

End Sub




__________________
Greg
………………………………………………
Work: XL2003/2007 on WinXP
Home: 02, 03 & 07 on Vista
Microsoft MVP - Excel (proof that truth is stranger than fiction)
Please use CODE tags - especially for longer excerpts of code.
Greg Truby is offline   Reply With Quote
Old Jan 20th, 2004, 11:20 PM   #10
BCS2
 
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
Default Re: Format Times in AM/PM

Great!! Thanks Greg.

Don't go too far. I am still testing this thing. I might need your assistance tomorrow if you are available.

Thanks again.
BCS2 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 07:46 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes