In a cell in Excel sheet I have a Date value like:
01/01/2010 14:30:00
I want to convert that Date to Text and also want the Text to look exactly like Date. So a Date value of 01/01/2010 14:30:00 should look like 01/01/2010 14:30:00 but internally it should be Text.
How can I do that in Excel?
=TEXT(A1,"DD/MM/YYYY hh:mm:ss")
(24 hour time)
=TEXT(A1,"DD/MM/YYYY hh:mm:ss AM/PM")
(standard time)
Here is a VBA approach:
Sub change()
toText Sheets(1).Range("A1:F20")
End Sub
Sub toText(target As Range)
Dim cell As Range
For Each cell In target
cell.Value = cell.Text
cell.NumberFormat = "#"
Next cell
End Sub
If you are looking for a solution without programming, the Question should be moved to SuperUser.
Here's another option. Use Excel's built in 'Text to Columns' wizard. It's found under the Data tab in Excel 2007.
If you have one column selected, the defaults for file type and delimiters should work, then it prompts you to change the data format of the column. Choosing text forces it to text format, to make sure that it's not stored as a date.
In some contexts using a ' character beforehand will work, but if you save to CSV and load again this is impossible.
'01/01/2010 14:30:00
Couldnt get the TEXT() formula to work
Easiest solution was to copy paste into Notepad and back into Excel with the column set to Text before pasting back
Or you can do the same with a formula like this
=DAY(A2)&"/"&MONTH(A2)&"/"&YEAR(A2)& " "&HOUR(B2)&":"&MINUTE(B2)&":"&SECOND(B2)
I have no idea about the year of publication of the question; it might be old now. So, I expect my answer to be more of a reference for future similar questions after my post.
I don't know if anybody out there has already given an answer similar to the one I am about to give, which might result -I think- being the simplest, most direct and most effective: If someone has already given it, I apologize, but I haven't seen it. Here, my answer using CStr instead of TEXT:
Asuming cell A1 contains a date, and using VBA code:
Dim strDate As String
'Convert to string the value contained in A1 (a date)
strDate = CStr([A1].Value)
You can, thereafter, manipulate it as any ordinary string using string functions (MID, LEFT, RIGHT, LEN, CONCATENATE (&), etc.)
If you are not using programming then do the following
(1) select the column
(2) right click and select Format Cells
(3) Select "Custom"
(4) Just Under "Type:" type dd/mm/yyyy hh:mm:ss
In Excel 2010, marg's answer only worked for some of the data I had in my spreadsheet (it was imported). The following solution worked on all data.
Sub change()
toText Selection
End Sub
Sub toText(target As range)
Dim cell As range
Dim txt As String
For Each cell In target
txt = cell.text
cell.NumberFormat = "#"
cell.Value2 = txt
Next cell
End Sub
As Text is localized it will break when trying you try to share your files over diffrent cultures. ÅÅÅÅ-MM-DD might work perfectly in sweden, is US, Germany or israel it will turn to shit.
The reasonable solution would be that english was accepted everywhere, but it's not.
Basically DON'T EVER use text as intended to format dates.
Here is how to create the date in ISO format. TEXT is used to ensure leading
=YEAR(A1)&"-"&TEXT(MONTH(A1);"00")&"-"&TEXT(DAY(A1);"00")
If you want it backwards, sideways or whatever, just change it.
https://www.reddit.com/r/ISO8601/comments/enhlp6/logic_of_the_different_date_time_systems_with/
The selected answer did not work for me as Excel was still not converting the text to date. Here is my solution.
Say that in the first column, A, you have data of the type 2016/03/25 21:20:00 but is stored as text. Then in column B write =DATEVALUE(A1) and in column C write =TIMEVALUE(A1).
Then in column D do =B1+C1 to add the numerical formats of the date and time.
Finally, copy the values from D into column E by right clicking in column E and select Paste Special -> Paste as Values.
Highlight the numerical values in column E and change the data type to date - I prefer using a custom date of the form YYYY-MM-DD HH:MM:SS.
Related
Using VBA I open one workbook, copy data and paste it to another workbook. In the second workbook I set a formula for specific range of custom format of values(dd/mm/yyyy hh:mm) but the formula doesn't work. Values are kept on the left side of cells as a text with custom format until I double click. Format in both workbooks(worksheets) is the same and I want to keep it.
I've been trying fix it using:
range.TextToColumns Destination:=range DataType:=xlFixedWidth FieldInfo:=Array(Array(0,9),Array(1,1), Array(16,9))
It works within halfway. Date is fit to the right side of cells but my day from custom date is 17 days less???:D When I omit Array(0,9) it returns additional date 02/01/1990 and my original date is moved to the next column.
Has anyone got any solution?
How exactly does .TextToColumns work? How Can I correctly declare arg for FieldInfo to receive original date and fit it to the right side of cells?
below is an example of how to convert a date that is in text format (cell B1) to an actual date. Notice how a text format is left aligned and a date format is right aligned.
Keep in mind that a date is a number-> the integer part is the date and the decimal part is the time.
Public Sub sTestDate()
Dim v As Variant
v = Sheet1.Range("B1").Value
Sheet1.Range("B2").Value = CDbl(CDate(v))
Sheet1.Range("B3").Value = CDbl(CDate(v))
Sheet1.Range("B3").NumberFormat = "dd/mm/yyyy hh:mm"
End Sub
I have cells containing a simple concatenation function with D24 as the previous year (e.g. 15) and a custom format (MMM JJ)
CONCATENATE("Apr ",$D$24)
When I am copying and pasting these cells with a VBA then "Apr 15" becomes "15.04.16" and because of the formatting "Apr 16"
Selection.Value = Selection.Value
Was is the reason behind this? Is there another solution than just changing the format to text?
Excel will generally try to convert anything that looks like a date into a real date (serial number where 1 = 1 Jan 1900). One way to avoid that, and remove the formula as you are doing above, would be to pre-format as text. So:
With Selection
.NumberFormat = "#"
.Value = .Text
End With
might do what you want.
There are other ways, but if you don't change the cell format to text, or prefix the entry with a single quote ', any subsequent editing of that cell, even inadvertent selection, raises the risk of converting it to the real date.
That depends on what you want in your cell. Dou you want a string or a date?
If you want a string:
either format as text or
add a ': CONCATENATE("'Apr ",$D$24)
if you want a date:
use the following formula instead of concatenate: =DATE($D$24,4,1)
If you simply Copy Paste it, only the Value is pasted not the formatting (if I remember right)
Try to avoid using Selection instead use Range.
And use Range.Copy and Range.PasteSpecial Paste:=xlPasteFormats so your formatting is pasted with the values.
I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192
It's very simple why your formulas doesn't work corectly. When yor're using somehting like this: RIGHT(A2,4), your value from A2 translates to 41579 for 1/11/2013 (Excel stores all dates as integers and all times as decimal fractions. You can read more here). Next formula should work well:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column):
=A2*1
and drag it down. Then copy values from this temp column and paste them using "Paste special->Values" in colunm A (where should be date format as well)
Or you can use this simple macro:
Sub test()
With Range("A2:A100")
.NumberFormat = "dd/mm/yyyy"
.Value = .Value
End With
End Sub
I have been searching for about an hour on how to do this in Excel.
I have an Excel file that was created from an old system and I am pulling information from a SQL Server Database, I will be inputting the information back into the SQL Server Database and would like the Dates to match.
I have tried Creating a Custom Format, but I am unsure if I even did it Correctly. I found several places where they want to go the other way mm/dd/yyyy to yyyymmdd but they have not been helpful.
I am unfamiliar with using VBA in any Microsoft Products otherwise I am sure that this would be a simple Task.
I have two separate columns that need to be changed.
How do I Format the entire column from (float)yyyymmdd to a (Date)mm/dd/yyyy
You can convert the value to a date using a formula like this, next to the cell:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2))
Where A1 is the field you need to convert.
Alternatively, you could use this code in VBA:
Sub ConvertYYYYMMDDToDate()
Dim c As Range
For Each c In Selection.Cells
c.Value = DateSerial(Left(c.Value, 4), Mid(c.Value, 5, 2), Right(c.Value, 2))
'Following line added only to enforce the format.
c.NumberFormat = "mm/dd/yyyy"
Next
End Sub
Just highlight any cells you want fixed and run the code.
Note as RJohnson mentioned in the comments, this code will error if one of your selected cells is empty. You can add a condition on c.value to skip the update if it is blank.
Do you have ROWS of data (horizontal) as you stated or COLUMNS (vertical)?
If it's the latter you can use "Text to columns" functionality to convert a whole column "in situ" - to do that:
Select column > Data > Text to columns > Next > Next > Choose "Date" under "column data format" and "YMD" from dropdown > Finish
....otherwise you can convert with a formula by using
=TEXT(A1,"0000-00-00")+0
and format in required date format
Here is a bare bones version:
Let's say that you have a date in Cell A1 in the format you described. For example: 19760210.
Then this formula will give you the date you want:
=DATE(LEFT(A1,4),MID(A1,5,2),RIGHT(A1,2)).
On my system (Excel 2010) it works with strings or floats.
for converting dd/mm/yyyy to mm/dd/yyyy
=DATE(RIGHT(a1,4),MID(a1,4,2),LEFT(a1,2))
Found another (manual) answer which worked well for me
Select the column.
Choose Data tab
Text to Columns - opens new box
(choose Delimited), Next
(uncheck all boxes, use "none" for text qualifier), Next
use the ymd option from the Date dropdown.
Click Finish
I have two cells in Excel. one has a string and the other one has a date. in the third cell I want to put the date and the string together. For example:
A1 = "This "
A2 = "03/03/1982"
I want A3 to be:
This 03/03/1982
when I try to put this in the A3 formula: = A1 & A2 it returns some funny numerical value for the date and does not give me the literal date.
Don't know if it's the best way but I'd do this:
=A1 & TEXT(A2,"mm/dd/yyyy")
That should format your date into your desired string.
Edit: That funny number you saw is the number of days between December 31st 1899 and your date. That's how Excel stores dates.
This is the numerical representation of the date. The thing you get when referring to dates from formulas like that.
You'll have to do:
= A1 & TEXT(A2, "mm/dd/yyyy")
The biggest problem here is that the format specifier is locale-dependent. It will not work/produce not what expected if the file is opened with a differently localized Excel.
Now, you could have a user-defined function:
public function AsDisplayed(byval c as range) as string
AsDisplayed = c.Text
end function
and then
= A1 & AsDisplayed(A2)
But then there's a bug (feature?) in Excel because of which the .Text property is suddenly not available during certain stages of the computation cycle, and your formulas display #VALUE instead of what they should.
That is, it's bad either way.
Another approach
=CONCATENATE("Age as of ", TEXT(TODAY(),"dd-mmm-yyyy"))
This will return
Age as of 06-Aug-2013
Thanks for the solution !
It works, but in a french Excel environment, you should apply something like
TEXTE(F2;"jj/mm/aaaa")
to get the date preserved as it is displayed in F2 cell, after concatenation.
Best Regards
You can do it this simple way :
A1 = Mahi
A2 = NULL(blank)
Select A2 Right click on cell --> Format cells --> change to TEXT
Then put the date in A2 (A2 =31/07/1990)
Then concatenate it will work. No need of any formulae.
=CONCATENATE(A1,A2)
mahi31/07/1990
(This works on the empty cells ie.,Before entering the DATE value to cell you need to make it as TEXT).
I found that for this situation, the simplest solution is to define a Custom number format for the cell containing the date. The format in this case should be:
"This:" mm/dd/yyyy
To set this format:
Right click on the cell
Select Format Cell
Select Number tab (should be displayed by default)
Pick Custom from the Category list
Specify the format in the "Type" field
Press OK
Note: If you really want the preceding text to be picked from a cell, this solution will not work as described.