Option Explicit
Sub LoadData()
Dim a, b
Dim dRng As Range, IDrng As Range, IDrngx As Range, rng As Range
Dim sDate As Date, fDate As Date
Dim sRow As Long, nRow As Long, lRow As Long, r As Long, idx As Long, idy As Variant
Application.ScreenUpdating = False
With Sheets("data")
.Activate
Set dRng = .Range("E5:J" & .Cells(Rows.Count, "E").End(xlUp).Row) ' Set DATA range
Set IDrng = .Range("E6:E" & .Cells(Rows.Count, "E").End(xlUp).Row) ' Personnel IDs
dRng.Sort key1:=Range("G1"), order1:=xlAscending, Header:=xlYes ' Sort by ascending Date
a = dRng ' Assign to array "a"
End With
ReDim b(1 To 1000, 1 To 62) ' Set ouput array : 62 columns for 31 days x 2 entries per day
With Sheets("Sheet1")
.Activate
.Range("F6:BN" & .Cells(Rows.Count, "D").End(xlUp).Row).ClearContents
Set IDrngx = .Range("E6:E" & .Cells(Rows.Count, "E").End(xlUp).Row) ' Personnel IDs
sDate = .Range("F3"): fDate = Application.EoMonth(sDate, 0) ' Month start date and month-end date
sRow = Application.Match(CLng(sDate), dRng.Columns(3), 0) ' Start row of data for this month
nRow = Application.CountIfs(dRng.Columns(3), _
">=" & CLng(sDate), dRng.Columns(3), "<=" & CLng(fDate)) ' Number of rows (entries) for this month
lRow = sRow + nRow - 1 ' Last row of data for this month
For Each rng In IDrngx
idy = Application.Match(rng, IDrng, 0) ' Check if iD is in "Data"
If Not IsError(idy) Then ' ID is in DATA
For r = sRow To lRow ' Loop through data for this month
idx = Day(a(r, 3)) - 1 ' index for day in output array
idy = Application.Match(a(r, 1), IDrngx, 0) ' index (row) for personnel ID
If Not IsError(idy) Then
b(idy, idx * 2 + 1) = a(r, 4) ' Result
b(idy, (idx + 1) * 2) = a(r, 5) & " " & Format(a(r, 6), "hh:mm") ' Day / time
End If
Next r
.[F6].Resize(nRow, 62) = b ' Output data
.Columns("F:BO").HorizontalAlignment = xlCenter ' centre in columns
.Columns("F:BO").ColumnWidth = 11 ' set column width
Else
MsgBox rng & " " & rng.Offset(0, -1) & " was not found in sheet DATA "
End If
Next rng
End With
Application.ScreenUpdating = True
End Sub
Bookmarks