![]() |
|
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
|
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 |
|
|
|
|
|
#2 |
|
MrExcel MVP
Int'l Moderator Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
|
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. |
|
|
|
|
|
#3 |
|
Join Date: Jul 2002
Location: Perth, Australia
Posts: 1,416
|
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 cant 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 |
|
|
|
|
|
#4 | |
|
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
|
Quote:
Thanks |
|
|
|
|
|
|
#5 |
|
MrExcel MVP
Int'l Moderator Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
|
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
__________________
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. |
|
|
|
|
|
#6 |
|
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
|
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? |
|
|
|
|
|
#7 |
|
MrExcel MVP
Int'l Moderator Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
|
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. |
|
|
|
|
|
#8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
|
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. ******** ******************** ************************************************************************>
[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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
#9 |
|
MrExcel MVP
Int'l Moderator Join Date: Jun 2002
Location: 39° 17' 15" N, -94° 40' 26" W
Posts: 8,796
|
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. |
|
|
|
|
|
#10 |
|
Join Date: Dec 2003
Location: Louisville, KY
Posts: 67
|
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. |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|