I am a beginner in VBA. I need solution to my problem only in VBA. Thanks.
Following are the tasks that I am required to do
Select a 'ID' and a 'Course' from the table. In the table below I have mentioned few IDs and Courses, however my project has multiple ID's and Courses. I cannot keep entering the ID and course name for all the cases. Is there a smart way to do this?
For a specific ID, Specific course and specific Date, I need to calculate the time difference between two rows as given in below table for specific 'Event'. The time difference first two rows give the time spent on 'Reading', the difference between row 2 and 3 give the time spent on 'Writing', the difference between row 3 and 4 give the time spent on 'Writing', and so on.
Any help will be highly appreciated. The data format is in the link below
You could modify the code and try:
Option Explicit
Sub test()
Dim strID As String, strCourse As String, strEvent As String
Dim DateTime1 As Date, DateTime2 As Date
Dim LastRow As Long, i As Long, y As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 2 To LastRow
strID = .Range("A" & i).Value
strCourse = .Range("B" & i).Value
DateTime1 = CDate(.Range("C" & i).Value) & " " & CDate(.Range("D" & i).Value)
strEvent = .Range("E" & i).Value
For y = 2 To LastRow
If y <> i Then
If .Range("A" & y).Value = strID And .Range("B" & y).Value = strCourse And .Range("E" & y).Value = strEvent And .Range("F" & i).Value = "" And .Range("F" & y).Value = "" Then
DateTime2 = CDate(.Range("C" & y).Value) & " " & CDate(.Range("D" & y).Value)
.Range("F" & i).Value = Abs(DateDiff("h", DateTime2, DateTime1))
End If
End If
Next y
Next i
End With
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.