VBA Date + TimeValue returns no time - excel

I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?

Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.

When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.

Related

Using Unix US Formatted Date-time-year for calculations in Excel

I have dates that are formatting using the default time formats for Bash (echo $date) for example:
Fri Dec 24 07:35:41 EST 2021
I cannot change this behavior as I don't have permissions to alter our IT solution. I need to be able to load these dates into a UK localised Excel, but any attempts to use the usual Text To Columns DMY approach doesnt work due to the Day, time zone, time etc.
Is the only way to extract the data and re-assemble it or is there a simpler solution?
Try this function . write it to module and call from any cell . Like =makeDateFromStr(A1)
Function makeDateFromStr(ByVal someDate As String) As Date
On Error Resume Next
Dim var As Variant, Mu As Long
var = Split(someDate, " ") ' array to hold all parts of the string-date
Mu = (InStr("JanFebMarAprMayJunJulAugSepOctNovDec", var(1)) + 2) / 3 ' a way to make short string month (Dec) to number
makeDateFromStr = DateSerial(var(5), Mu, var(2)) & " " & TimeValue(var(3)) ' putting all together
On Error GoTo 0
End Function
As far as I can see, if we get rid of the EST and the week day FRI, we can get a valid date value. Note: If you want the weekday, you can use the function WEEKDAY() to get the value 6, which is Friday. (1 is Sunday).
So, to get rid of the weekday, you can use the RIGHT() function.
All weekdays are 3 letters and a space, so we need to get rid of the 4 first characters, like so:
=RIGHT(A1;LEN(A1)-4)
You can put this just underneath your example time, so place this in cell A2.
To remove the EST is a bit more tricky, we need to know where in the text EST is. For this, we use FIND():
=FIND("EST ";A2)
Now we know at which position the text "EST " appears in A2.
Then we can use a simple REPLACE() function, to replace "EST " with nothing. Like so:
=REPLACE(A2;FIND("EST ";A2);4;"")
Note: The 4 refers to the length of "EST ".
This worked for this example, so you can try it out with different data. Just comment below if it isn't working correctly, and with what data and I'll look into it.
Put a date in A1
Put =RIGHT(A1;LEN(A1)-4) in A2
Put =REPLACE(A2;FIND("EST ";A2);4;"") in A3
You could also merge it into 1 cell:
=REPLACE(RIGHT(A1;LEN(A1)-4);FIND("EST ";RIGHT(A1;LEN(A1)-4));4;"")
You could then place this next to the date in A1, so in B1, and drag this formula down for ALL your imported dates. Now it should work for all of your dates.
You could also copy their values and replace the original dates, then remove the formulas, and you will have the same layout and functionality you were expecting from the beginning!

Difference time view from Excel to VBA

I can't obtain the average time from start to end of some activities,
I tried 1K way but the result isn't correct, every time I've one day minus.
the image can explain better (that my english).
In this example the sum of my activities il 480:52:56 hours, in vba I've different result, for vba the date is "19/01/1900 00:52:56" like 456:52:56 hous
24 hours minus
why this difference? and how I can obtain the same result?
thanks for any suggestion
Dates are stored as serial numbers where first valid date has a value of 1. This value in excel reads as 01/01/1900, and in VBA as 31/12/1899. In excel, value 60 returns 29/02/1900 which doesn't exist in VBA, so from value 61 onwards all values will return the same date in VBA and excel.
/e: Also, maximum value is 2958465 (31/12/9999), values higher than that will return error rather than valid date
thanks to your comments I understand that the problem is for the minor dates of March 1, 1900 so I changed the select from:
Select [DataAttesa] as Data, avg(iif([totHours] > 1 and [totHours] < 61, dateadd("d",-1,CDate([totHours])) , [totHours])) as nr FROM [db_In$] Where TypeTrasp = "AOG" group by [DataAttesa] Order by [DataAttesa]asc
now, when I put the recordset.results on excel the value are correct.
Thank at all

Handling TimeRecording-files in Excel (formatting cells)

Excel's assumptions about cells are confusing the heck out of me. I'm on Office 365 - Excel for Mac, Version 15.28.
I'm TimeRecording on a lot of things, I would like to calculate relations and tendencies on the different things. I've exported my log-files, and have opened it in excel. A simple version looks like this:
In the real sheet, then I have 40+ tasks and 50+ dates. I would like to be able to do some calculations on these data. But Excel doesn't 'know what it is' (time durations) and therefore can't add them up or do anything.
So one question would be, to how to let Excel know, that this is time durations? I tried doing what this question suggests. But when I format the cells as [h]:mm then it gives me this error:
FYI: In the big sheet, then there's so many times, so the total amounts up to something along the lines of 633:33.
I would just like to be able to do simple calculations, such as:
=SUM(B1, C4, D5)
or
=SUM(B1, C4, D5)/COUNT(B1, C4, D5)
And maybe also make some charts and graphs.
Another attempt I've done is to try to get all the cells to have the format hh.mm instead of hh:mm, but this gave me problems. My approach was this:
Convert all the cells to 'Text' to tell Excel: 'Hey... Don't do any auto-converting/guessing here, and don't turn any of the cells into dates or decimal numbers or fricking origami swans!'
After that then I make a simple 'Replace all' of : to .
But after the 'Replace all', then 633:33 turns into 633.36.00 (even though the cell was a 'Text' cell).
And if I then simply double-click on the cell to edit it, then the numbers 'magically' turns from 633.36.00 to 27/01/1900 15.36.00 ... What the hell!? I need a procedure that doesn't require me to go through all my thousands of numbers and edit any of them (or ensure that Excel have turned the numbers into flying unicorns.
EDIT1
Here's an example of the total sheet I'm working on in Google Sheets.
EDIT2
If I format the cells as [h]:mm, then I get an error (see above). But if I format it as [t]:mm, then I don't get an error (thanks to Axel Richter for pointing that out). It may have something to do with the initial language of my Excel-installation (danish).
However... If I then try to sum up a bunch of cell, after doing this formatting to everything, then it sums up to 0:00.
If I format all the cells to Time (well-knowing that it's the wrong format, but hoping that Excel can see it and fix it) - and thereafter trying to sum up a couple of cells, then it sums up to 00.00.00 (even though it wasn't empty cells).
Is it also important, that when I sum up some numbers, that I do it from a General-cell - or does Excel know, that if I start with the =-sign, that it's going to be a calculation (and therefore the cell-format doesn't matter)?
Excel will store date-time values as floating point double values in following form:
1 day = 1
1 hour = 1/24 = 0.0416666666666667
1 minute = 1/24/60 = 0.000694444444444444
So formatted as time all values greater than or equal 0 but lower than 1 will be from 00:00 to 23:59. Values greater than 1 will be dates with 1 = 01/01/1900 00:00:00. But if you are formatting such values as time only using hh:mm for example, then only the time is shown. The date is simply hidden.
For example 1.25 formatted using hh:mm will show 06:00 although it is 1 1/4 day which is 01/01/1900 06:00:00. To see hours from multiple days the format [h]:mm can be used. For example 1.25 formatted using [h]:mm will show 30:00 which is 1 day (24:00) + 1/4 day (06:00).
Although Excel will do this independent of locale settings, the user defined format codes used and the kind of input values which Excel will take as time values are dependent of locale settings.
For example with your locale Danish (Greenland) the format codes are different. See Formatere tal som datoer eller klokkeslæt .
So your format code will be [t]:mm instead of [h]:mm.
And also with your locale Danish (Greenland) the time separator is . instead of :. So values which Excel will take as time values are 123.45 (123 hours, 45 minutes) instead of 123:45.
In your last comment you say: "whereafter it weirdly looked the same, such as: 123:45 and not 123.45". Yes that is because your user defined format [t]:mm contains the time separator : also. But that is different from your locale settings where . is the time separator. While inputting values Excel respects the locale settings and so expects 123.45 as time value for 123 hours and 45 minutes. But after the input Excel applies the cell formatting [t]:mm and so shows 123:45.
In your last comment you say that it confuses you that 17:24 * 24 equals 417:36. But that is exactly what it should.
17:24 is 17 hours and 24 minutes. That multiplied by 24 is 17 hours * 24 = 408 hours and 24 minutes * 24 = 576 minutes. 576 minutes are 9 hours and 36 minutes. So we get (408 hours + 9 hours) and 36 minutes = 417 hours and 36 minutes = 417:36.
I cannot edit the sheet so I copied it. As you can see in column AS and row 43, Google provides 'duration' format. You don't have to manipulate something. Just change the cell format.
In Excel, duration format is [h]:mm. Hit ctrl + 1 at the cell and choose Custom and type [h]:mm at Type and hit enter.
If SO answer is too difficult to follow, try this.
I apologize in advance for how rough this is, but I mostly slapped this code together to fit the task and didn't want to waste time on it. The principles are there though, so at the least it should point you in the direction you need to be heading.
Sub Time_Summarization()
Dim i As Long
Dim j As Long
Dim cell As Range
Dim sHolder As String
Dim vHolder As Variant
Dim arrHolder() As Double
Dim bAdd As Boolean
Dim dHolder_Whole As Double
Dim dHolder_Remainder As Double
Dim sOutput As String
ReDim arrHolder(0 To 2)
' Use a set range. Selection here is just for testing
' Ideally there should be data validation in this loop to ensure that the input
' values are numeric time values.
For Each cell In Selection
' Convert the cell value to a date to permit splitting.
' The value is then split into a 1-d array with 3 positions (H, M, S)
vHolder = Split(CDate(cell.value), ":")
' Loop through the split values from first to last, and trim off the AM/PM.
' If it is a PM date, set the flag to add 12 (13:00:00 gets displayed as 1:00:00 PM)
For j = LBound(vHolder) To UBound(vHolder)
' If PM, set the flag.
If InStr(vHolder(j), "PM") Then bAdd = True
' Remove "AM" and "PM"
vHolder(j) = Replace(vHolder(j), " AM", vbNullString)
vHolder(j) = Replace(vHolder(j), " PM", vbNullString)
' Add the values into the array in the same order.
arrHolder(j) = arrHolder(j) + vHolder(j)
Next
' Add 12 hours if needed
If bAdd Then arrHolder(0) = arrHolder(0) + 12
' Reset the flag for the next loop
bAdd = False
Next
' Step backwards through the array to round up increments of 60.
For i = UBound(arrHolder) To LBound(arrHolder) + 1 Step -1
' This will return the number of times the value goes into 60.
dHolder_Whole = arrHolder(i) \ 60
' This will return the remainder of the value divided by 60.
dHolder_Remainder = arrHolder(i) Mod 60
' Round up seconds to minutes, and minutes to hours.
arrHolder(i - 1) = arrHolder(i - 1) + dHolder_Whole
' Overwrite the remainder
arrHolder(i) = dHolder_Remainder
Next
' Combine the separate values into a string.
sHolder = arrHolder(0) & ":" & arrHolder(1) & ":" & arrHolder(2)
' Just for testing, do with the values whatever you wish.
Debug.Print sHolder
End Sub
Again, this is mostly a model that will work, but will need to be adapted to suit your needs.
Zeth, I downloaded your file and I can make some calculation with your time data. I juss selected all cell with time duration and change the format of cell to "time". Seemingly you should change all cells format, incluiding the empty cells.
If it does not work, find the "More format of numbers" ate the "Numbers" menu. Then, select the option "Hour" and chose the format closest to the format of your data. Also pay attenction to the option "locality" at the bottom of this menu. The option of hour format deppends on the region selected. (Each region in the world have some convenctions about it and Excel reconize much of then.
Formatting the numbers does not change the way Excel does calculations.
So a cell (c3) formatted as time and showing 01:28:00 actually contains 0.061111 because Excel treats time as fractions of a 24 hour day.
When you add up a lot of times and they add up to more than 24 hours the underlying number is more than 1 day so you get number of days before the decimal point and after the decimal point is the fraction of 24 hours remaining. So to convert a duration or time to hours you just multiply it by 24 and format it as a number or general (and the numbers after the decimal point are fractions of an hour). If you just want to format the result as hours and minutes use a format of [h]:mm and do not multiply by 24 - on your system look at Format Cells - Custom to see what the equivalent of [h:mm] is.

use IF() with TIMEVALUE() in excel

I have the folowing formula:
=IF(TIMEVALUE("14:30") - TIMEVALUE(NOW()) < TIMEVALUE("00:00"),"Past","Future")
Excel is giving me ERROR, i checked all formulas individually and they all give me the time value (which in theory should be enough to compare with an IF statement).
How come that i keep on getting error. Some cell formats not correct or something? Any help is appreciated!
Try to evaluate the formula step by step to see, if this is what you want.
Working formula:
=IF(TIMEVALUE("14:30") - NOW() < TIMEVALUE("00:00");"Past";"Future")
Evaluated arguments, using [F9 key]:
=IF(0,604166666666667 - 42719,6943635416 < 0;"Past";"Future")
You will always get the "Past" as returned value.
Explanation of date and time in Excel
The TIME in Excel is a proportion of a day. "00:00" = 0.00 and "24:00" = 1.00. Other values of TIME are DECIMALS between 0 and 1.
The DATE is a number of days since the first day. "1900-01-01" = 1 and "2000-01-01" = 36526. It is always an INTEGER.
Combining DATA and TIME (like in NOW() function) gives you an INTEGER + DECIMAL. When I evaluated the NOW() it returned 42719,6943635416
The TIMEVALUE function expects a STRING/TEXT that is considered a TIME and converts that STRING/TEXT to DECIMAL between <0; 1>.
Links
MS - How to use dates and times in Excel
Trump-Excel:Identify Errors Using Excel Formula Debugging (2 Methods)
Convert the Now to string first, using Text function, then it will work as expected.
TEXT(NOW(),"HH:MM:SS")
Final formula:
=IF((TIMEVALUE("11:30") - TIMEVALUE(TEXT(NOW(),"HH:MM:SS"))) < TIMEVALUE("00:00"),"Past","Future")

VBA dates collected correctly but output wrongly

I grab dates from one spreadsheet and output them onto another spreadsheet. After grabbing the date, when I debug.print it is in the correct format. When I output the date, debug.print also displays the correct format. However, the format on the spreadsheet the value has just been sent to, doesnt show the correct format.
I am using:
Sheets(SheetName).Cells(RowNum, ColNum).Value = Data
Sheets(SheetName).Cells(RowNum, ColNum).NumberFormat = "dd/mm/yyyy"
after I have pasted the value, but the months and days are still switched the wrong way.
Is there something I am doing wrong?? If I right click the cell it thinks it's date is dd/mm/yyyy but instead of 4th Sept it is showing 9th April.
This might be some trouble with localization:
Try using NumberFormatLocal, if DanielCooks tip didn't help ;)
edit: erlier it was statet by mister Cook, to check if the given data is correct.
edit:
With my german version I have quite some trouble to use / as the seperator, that is why i tryied with this code .NumberFormat ="dd-mm-yyyy;#" - works fine; I can switch days and month as I like.
edit:
With .NumberFormatLocal = "TT/MM/JJJJ" I have to use the german shorts for day, month and year, but now I can use / as the seperator.
You should experiment a litte bit with some formats strings ;)
Sorry to resurrect an old post, however I had a problem with VBA outputting a valid date as US style with the actual date changed for example 1st May changed to 5th Jan. I came upon this post but I didn't find the answer I needed here and now that I have worked it out I thought I would share it:
The key is not to define the variable storing the date as a "date" but as a "double", e.g.
Dim ReportDate As Double
ReportDate = Date
Range("E6").Value = ReportDate
This works as it outputs the numeric "date value" which excel then formats locally e.g. 41644 gets formatted as "05/01/14" using UK format or "01/05/14" using US format.
Hope this proves useful to other people (probably me when I forget how I solved it in six months time).
In the end I had to format the cell as "TEXT" to keep the correct format
(1) You need to define the variable to "Date" type to read the input, then set the date format before assigning it to the date variable.
(2) You also need to format the date output to make it work !
'**Read the date input****
Dim date1 as Date
Range("B2").NumberFormatLocal = "dd/mm/yyyy"
date1 = Range("B2").Value
'**Output the date****
Range("E2").Value = date1
Range("E2").NumberFormatLocal = "dd/mm/yyyy"

Resources