Using VBA, I import a csv file and put a bunch of data into several columns.
One of these columns has a date and time. As I need to be able to use just the 'time' part of these cells, I try to convert the entire column to Time by using (and just about every other variation)
Cells(x.y).EntireColumn.NumberFormat = "hh:mm:ss"
or
Range("C1").NumberFormat = "hh:mm:ss"
Range("C1").EntireColumn.NumberFormat = "hh:mm:ss"
However, this does not convert the entire column. I've tried every possible other way of selecting the entire column and changing it (through VB) however still only a portion remains converted.
If I doubleclick on these unconverted cells and press enter they change to the correct format. I realise this is a common problem relating to Calculations but my workbook is set to Automatic Calculations and I've tried setting this in VB too. This doesn't change anything.
The only pattern I can find is that the cells stop being converted when the Day reaches double digits. For example:
Column C
01/05/2013 7:28:56
03/05/2013 13:24:53
07/05/2013 20:13:24
09/05/2013 8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
Becomes:
Column C
7:28:56
13:24:53
20:13:24
8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
In the formula bar up the top for each cell it still shows the whole Date and Time for all cells, not sure if this is related, but doesn't seem to matter in terms of the calculations i have to perform using the Time.
Essentially I have to take the time for a cell in column C and the time from another Cell (also in Date/Time format) and check the difference. After some research I decided the best way was to convert all the cells to a time format and then do my calculations.
Alternatively I could try converting the column to text and using a Split function (using space as a delimiter) and pulling the time out, but I'm having trouble doing this too, as once again trying to convert the entire column to text stops at the double digits for date.
Thanks for reading through all that, any thoughts and help would be appreciated.
Edit: Realised some of my syntax was incorrect in my post, this was however correct inside my macro
another edit: I think this definitely has something to do with the date format... I just realised that before i format them, the dates are m/dd/yyyy and then when it gets to actual double digit days it changes to dd/mm/yyyy, and thats when the problem occurs...
In order to avoid confusion, and as the date always seems to occupy the same width, I recommend to
1) import this column as a text
2) then go over the whole column
For Each C In Range("A:A").Cells
If C <> "" Then
' ....
End If
Next C
3) cut away the leading 11 positions, e.g. C = Mid(C, 11, 99)
4) convert the remaining string to a time, e.g. C = CDate(C) (... yes it works with a time as well, because a time is a fractional part of a date)
Alternatively you may want to capture the date part and bring it into shape, too. See here for some ideas using worksheet formulas.
Related
We have an internal software that exports data reports to Excel, and several of the columns contain dates in the MM/DD/YYYY format. I have a spreadsheet setup where I will copy these exported reports over to, and then I have formulas already setup to look for these dates. The problem currently is that all dates in months 1-9 come in like this: 01/22/2017.
The formulas do not recognize them until I activate each and every cell, and hit enter, and then it re-formats to: 1/22/2017 and then everything works. Currently I am having to go cell by cell and activate, and hit enter. Simply selecting all of the cells, and changing the formatting to a Date hasn't worked. Is there a faster way around this? I'm open to VBA if it works, however I'm wondering if there is a simpler method I'm missing, or simply a way to get my formulas to recognize the original date formatting.
EDIT:
The data is exported from a SQL database. The formulas that need to reference are using <= in reference to week start dates on another sheet. I initially tried having those match this formatting, but because the dates come as General, math operators don't work at that point.
Select the date column and then click Data > Text to Columns
Next, Next, then select Date 'MDY'
then you should be able to do this using a number format of m/dd/yyyy or m/d/yyyy if you don't want a leading zero in the days as well
select the range and run:
selection.value = selection.value
(for some cases this does not work)... in such moments this should do:
selection.value = evaluate("INDEX(" & selection.address & "+0,)")
just keep in mind that there may be a need of changing the formatting of the cell...
This also can be done in a formula, without having to go through menu options, thus making it more automatic. Use TEXT and command and format the date exactly how you want. Leaving off leading zeros on the month or day is as simple as including just one character for them instead of two:
=TEXT(date-field,"M/D/YYYY")
instead of
=TEXT(date-field,"MM/DD/YYYY")
I have a calculation sheet, that checks if the rawdata has nothing in it, and if not, it will print the value of the cell.
Somehow, the timeformat is now left centered even though the cell-format and everything is the same as the above data.
I have tinkered with it for hours now and found no solution, so here i am hoping that someone knows what's going on.
SAMPLE FILE: https://ufile.io/dpe9u
Raw Data:
Calculated Data:
Result:
In the calculated data the (local, "HVIS" means "IF")formula is:
=HVIS('80 Rådata'!H951="";"";'80 Rådata'!H951)
I checked formatting and other settings, and everything seems the same, i even formatcopied from the rows where "it worked".
The data in column H sheet "80 Rådata" contains a few time values, but mainly text values that look like time values. Similar in column I, and J and K are all text values that look like times.
You can see that when you select the values and change the cell format to "General". Those that still look like times are text.
To fix that, select one of the columns, then click Data > Text to columns > Next > Next. In this step 3 of the wizard, specify that the data is a date (yes, a date). Then click Finish. Now all values are real time values, which you can verify by formatting as General again (then format back to time).
Repeat that with each column in turn.
From what I can see, the text values all have two zeros in the hour position. 00:01:30. If they had only one zero like this 0:01:30 then Excel would recognize them as time. If you don't want to repeat the data cleanup you may want to change your original data source to produce the correct format.
My data is extracted from an application and it has a text that looks like a date/time in excel. How do I actually convert "3/24/2016 11:22:07 PM" (in text) to a real date/time conversion? I've tried formatting the cells but it doesn't work.
For a date conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))
For a time conversion:
=TIMEVALUE(TEXT(A1,"HH:MM:SS"))
For datetime conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))+TIMEVALUE(TEXT(A1,"HH:MM:SS"))
Where A1 has the data you wish to convert.
By the way, then you may wish to format the cell to a date/time or whatever.
Hope that helps.
1) try using the DATEVALUE function and see if that works for you.
2) A more reliable way, since datevalue does not always work is to strip the text out manually and insert it into and excel date value. You are going to want to use a combination of the following functions:
DATE
TIME
IF
FIND
MID
LEFT
RIGHT
LEN
Now in my opinion the easiest way to do this is to work with multiple helper columns to build out all the steps. One column per step. When you get your final answer, you can substitute or copy paste your formulas from the helper columns into the final formula until you are left with one variable. The reason I say this is that the final formula referring to only 1 variable gets rather lengthy/ugly and very hard to trouble shoot if you make a typo, forget a bracket or something goes wrong. When I did this approach I used a totally of 14 columns (includes final formula). When I packed it all up into 1 formula it resulted in this:
DATE(RIGHT(LEFT(A3,FIND(" ",A3)-1),4),LEFT(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))-1),MID(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1,FIND("/",LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1)-FIND("/",LEFT(A3,FIND(" ",A3)-1))-1))+TIME(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)+IF(AND(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)<12,RIGHT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),2)="AM"),0,12),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1,FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)-FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)+1,2))
Note it is set up using cell A3 as the one with the time as text that needs formatting.
3) You should also be able to use excel's text to column function located on the DATA ribbon about half way across.
4) And of course there will be a way to code it through VBA as an option as well.
=DATEVALUE(A1)+TIMEVALUE(A1) seems to work as well, since each function only returns the value corresponding to what it recognizes in the string. That is, DATEVALUE() ignores the time component, while TIMEVALUE() ignores the date component.
I have an excel document with about 500 rows.
I need to format all the cells in , let's say, B column from date to text.
This is how it looks like now:
2012-06-15
2012-06-01
2012-06-14
What it looks like when formated to text:
41075
41061
41074
It has come to my understanding that this is a timestamp representing days since 1st januari 1900, right? Anyhow, this is not what I want. I want the value of the field to be exactly what it is but with the column type of text. I've found various solutions for this using functions like this: =TEXT(B1, "yyyy-mm-dd") but that is not reformating the cell, it is extracting a value from one cell, reformat it and represent is as text in another.
The rule I'm after: I want all cells in B column to have the type text without changing the value
Thanks!
If you have a situation where columns A to D are dates of 500 rows you then:
Use the =TEXT(A1, "yyyy-mm-dd") function you describe in cell E1.
Copy this formula 4 columns wide, and 500 rows down.
Then copy and paste values to the preceding cells.
Copy Example:
Output:
You're right, Excel stores dates internally as number of days since January 1st, 1900 (apart from a minor leap year bug).
Thus, I'm afraid you cannot have both:
Either you keep the value e.g. (41075) and simply format it as a date, so it'll be displayed as 2012-06-15 -
Or you convert it to text format - but then you either
Lose the underlying value - if you convert it to the format you wish with a text function as you mentioned
Keep the value (41075), but cannot see the date
If you are typing in the values you can by adding a ' before the values to keep it as text.
e.g.
But depending on the method the third party service uses to import these values this may not work and I bet you will not get around it unless you export it to a text editor and import it again.
Also try to play with diferent types of text for your third party service, e.g. "2012-06-15" as some see the quotes and remove them.
I have an Excel spreadsheet with a range of values which are numbers that go to up 20 decimal places, unpivoted from another sheet using the trick from here.
The trouble is the cells are only displaying 10 digits so, for example, even though the value is 5.46827166811115 it is showing as 5.468271668.
I've tried setting the format to text but it still wants to treat it as a number, the number of decimal places varies so I can't use a fixed #.### format. The only way I can get it to show is to format the cells as text and to just select and then click in the entry box for each and every cell!
It then shows a warning that the number in the cell is formatted as text or preceded by an apostrophe but at least it's showing the full value.
I did find a VBA script that just did something stupidly simple like cell.Value = cell.Value for the selection which seemed to work but I can't find it anymore and I can't reproduce that now.
Surely there's an easier way to do this? It wouldn't matter so much but when I import this data through SSIS into a VARCHAR(MAX) it's getting the truncated values!
Pre-pend a single apostrophe ' to the data. In many cases, this is more effective than setting the cell format to text.
You could format the cell as text and the do an .AutoFit so the cell expands to show all the cell content, like this:
Columns("A:A").EntireColumn.AutoFit
that will expand the A:A cell so all its content is visible.
try formatting with #.000 instead of #.###, but if your problem is that Excel is dropping precision on you, you could try multiplying the value by 10^20, then dividing by 10^20 on the SQL side.
found more info:
here
looks like Excel is limited to 15 digits of precision, multiplying by 10^20 doesn't increase the precision, so you can treat it like text or split the remaining digits into another column and combine them back with SSIS or SQL.
Have you added the IMEX=1 to your connection string? That keeps SSIS from trying to figure out the data from the first few rows.
Also, what about using a decimal datatype instead of varchar(max)