Date & time formatted on 2 lines in 1 cell

crug

Board Regular
Joined
Jan 14, 2006
Messages
56
Hello
Apologies if this has been solved previously - i have a number of dates on Sheet1, ie (=NOW() +1/48) and use those in Sheet2 as a dropdown list. The cell it goes into on Sheet2 is not wide enough for (06/09/2022 13:00:00) on 1 line. I need it to be Date & Chr(10) & Time as below.
06/09/2022
13:00:00

I cant seem to format using Format Cell correctly and wondered if i could with vba?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Maybe:
VBA Code:
Sub test()
    Range("A:A").Replace "  ", vbNewLine
End Sub

I left two spaces as that is what is usually there when we have Date & Time
 
Upvote 0
In VBA perhaps this:
VBA Code:
format(date ,"dd/mm/yyyy") & vbnewline & format(time,"hh:mm")

In a formula putting in Alt+Enter seemed to work.
Excel Formula:
=TEXT(NOW(),"dd/mm/yyyy 
hh:mm")
 
Upvote 0
Thank you, the VBA solution does what i need except the format is
9/6/2022
2:00:00 PM

I've tried to format it - but it still returns the PM
[Range("E16").NumberFormat = "dd/mm/yyyy hh: mm]
 
Upvote 0
I dont think i can use the Formula solution as i take the dates from a different sheet via a dropdown - that has date add calculations included
 
Upvote 0
OK you need to decide if you want the date as text or as a date.
As text you need to do all your formatting using the Format statement.
Once it is Excel as Text the Custom number formatting won't work since it only applies to numbers.

If you want to enter it into a cell as a real date (number) then you need something like this:
VBA Code:
Sub EnterDateTime_2Lines()
    Dim rng As Range
    Set rng = Range("A10")
    rng = Date + Time
    rng.NumberFormat = "d/mm/yyyy " & Chr(10) & "h:mm"    
    rng.WrapText = True
    rng.EntireRow.RowHeight = 29
End Sub
 
Upvote 0
Another option (better as the date and time remain a number) is to format (or formula) the cells to have a few spaces between the date and time. Make sure you have textwrap set to on and you will get the below as long as the whole date/time string does not fit in the cell. See below:
Book1
AB
101/01/2022 13:0001/01/2022 13:00
201/02/2022 13:0002/01/2022 13:00
301/03/2022 13:0003/01/2022 13:00
401/04/2022 13:0004/01/2022 13:00
501/05/2022 13:0005/01/2022 13:00
601/06/2022 13:0006/01/2022 13:00
701/07/2022 13:0007/01/2022 13:00
801/08/2022 13:0008/01/2022 13:00
901/09/2022 13:0009/01/2022 13:00
1001/10/2022 13:0010/01/2022 13:00
Sheet1
Cell Formulas
RangeFormula
B1:B10B1=TEXT(A1,"dd/mm/yyyy")&" "&TEXT(A1,"hh:mm")
 
Upvote 0
They look that way on the result of XL2BB but see below, if it was text then it should not let me add a day to it.
Cell Formulas
RangeFormula
B1:B10B1=TEXT(A1,"dd/mm/yyyy")&" "&TEXT(A1,"hh:mm")
C1:C10C1=B1+1
 
Upvote 0
Interesting but not without risk.
Excel seems to be converting it on the fly if you apply an arithmetic operator to it but ISTEXT considers it Text and a Pivot Table considers it text, so does a Vlookup.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,724
Members
449,465
Latest member
TAKLAM

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top