I want to calculate the time spent in column E by subtracting the Start stamp (column A) from the End stamp (column B).
What works fine with the formula in column H doesn't work with VBA.
When I uncomment the "Answers(i, 3) = DateEn(i, 1) - DateSt(i, 1)" line then the update of the month and year in columns C and D works fine.
So why the error for the update of time spent (column E)?
What works fine with the formula in column H doesn't work with VBA.
When I uncomment the "Answers(i, 3) = DateEn(i, 1) - DateSt(i, 1)" line then the update of the month and year in columns C and D works fine.
So why the error for the update of time spent (column E)?
VBA Code:
Dim WS As Worksheet
Dim DateSt() As Long
Dim DateEn() As Long
Dim Answers() As Long
Dim D1 As Long
Dim i As Long
Dim l As Long
Set WS = Worksheets("Data")
Range("A1").Select
Selection.End(xlDown).Select
l = Selection.Row
DateSt = WS.Range("A2:A" & l)
DateEn = WS.Range("B2:B" & l)
D1 = UBound(DateSt, 1)
ReDim Answers(1 To D1, 1 To 3)
For i = 1 To D1
Answers(i, 1) = Month(DateSt(i, 1))
Answers(i, 2) = Year(DateSt(i, 1))
Answers(i, 3) = DateEn(i, 1) - DateSt(i, 1) ' This line generates the error. When commenting this line out then the update of the month and year in columns C and D works fine.
Next i
WS.Range(Range("C2"), Range("C2").Offset(D1 - 1, 2)).Value = Answers
Arrays-VBA-Error.xlsm | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | |||
1 | Start | End | Month | Year | Time Spent | Formula | Time Spent = End - Start | ||||||
2 | 9/01/2024 10:51:10 | 9/01/2024 11:24:18 | 0,02300926 | ||||||||||
3 | 9/01/2024 11:25:19 | 9/01/2024 11:32:45 | 0,00516204 | ||||||||||
4 | 10/01/2024 10:12:10 | 10/01/2024 10:16:55 | 0,00329861 | ||||||||||
5 | 10/01/2024 10:40:03 | 10/01/2024 10:41:42 | 0,00114583 | ||||||||||
6 | 10/01/2024 10:42:50 | 10/01/2024 10:52:15 | 0,00653935 | ||||||||||
7 | 10/01/2024 11:10:18 | 10/01/2024 11:14:06 | 0,00263889 | ||||||||||
8 | 10/01/2024 11:20:12 | 10/01/2024 11:23:57 | 0,00260417 | ||||||||||
9 | 10/01/2024 11:24:44 | 10/01/2024 11:28:26 | 0,00256944 | ||||||||||
10 | 10/01/2024 11:30:34 | 10/01/2024 11:36:04 | 0,00381944 | ||||||||||
11 | 10/01/2024 11:37:40 | 10/01/2024 11:41:32 | 0,00268519 | ||||||||||
12 | 10/01/2024 13:24:57 | 10/01/2024 13:28:05 | 0,00217593 | ||||||||||
13 | 10/01/2024 13:28:05 | 10/01/2024 13:29:26 | 0,0009375 | ||||||||||
14 | 10/01/2024 13:29:26 | 10/01/2024 13:30:45 | 0,00091435 | ||||||||||
15 | 11/01/2024 8:13:51 | 11/01/2024 8:14:38 | 0,00054398 | ||||||||||
16 | 11/01/2024 9:34:05 | 11/01/2024 9:40:55 | 0,00474537 | ||||||||||
17 | 11/01/2024 9:40:55 | 11/01/2024 9:42:44 | 0,00126157 | ||||||||||
18 | 11/01/2024 10:08:25 | 11/01/2024 10:13:07 | 0,00326389 | ||||||||||
19 | 11/01/2024 10:31:13 | 11/01/2024 10:34:17 | 0,00212963 | ||||||||||
Data |
Cell Formulas | ||
---|---|---|
Range | Formula | |
H2:H19 | H2 | =B2-A2 |