Option Explicit
Global dict As Object
Function Fu_TS_DictLookup(EmpIdRNG As Range, Optional ReReadData As Boolean = False) As Variant()
Dim EmpID As Long
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
If dict Is Nothing Or ReReadData Then ' Reading Data from Sheet
Debug.Print "No data on memory or data refresh demanded, reading data"
' Creating variables
Set dict = CreateObject("Scripting.Dictionary")
Dim IdRNG As Range, c As Range
' Range where to find all "Emp ID"s
Set IdRNG = ws.Range("D2:D" & ws.Cells(Rows.Count, 4).End(xlUp).Row)
' Read all data to dictionary
For Each c In IdRNG
dict(c.Value) = WorksheetFunction.Transpose(WorksheetFunction.Transpose(c.Offset(0, -3).Resize(1, 7).Value)) ' c.value(add key if missing) = range.values to transform to array
Next
Else ' Data is allready written to Global dict
Debug.Print "Data allready in Dictionary"
End If
If EmpIdRNG.Cells.Count > 1 Then
'Creating variables
Dim i As Long: i = 1
Dim FuRows As Long: FuRows = EmpIdRNG.Cells.Count
Dim RetArrD2 As Variant
ReDim RetArrD2(1 To FuRows, 1 To 5)
For Each c In EmpIdRNG
EmpID = c.Value
RetArrD2(i, 1) = dict(EmpID)(2)
RetArrD2(i, 2) = dict(EmpID)(3)
RetArrD2(i, 3) = dict(EmpID)(7)
RetArrD2(i, 4) = dict(EmpID)(6)
RetArrD2(i, 5) = dict(EmpID)(5)
i = i + 1
Next
' The return value of Fu_WriteBack is a 2-dimensional RetArrD2 array
Fu_TS_DictLookup = RetArrD2
Else
' Store EmpIdRNG.value in EmpID to avoid repeated reading from sheet
EmpID = EmpIdRNG.Value
' Reading the desired data from the Dictionary into the Array
Dim RetArr(1 To 5) As Variant
RetArr(1) = dict(EmpID)(2)
RetArr(2) = dict(EmpID)(3)
RetArr(3) = dict(EmpID)(7)
RetArr(4) = dict(EmpID)(6)
RetArr(5) = dict(EmpID)(5)
' The return value of Fu_WriteBack is a 1-dimensional RetArr array
Fu_TS_DictLookup = RetArr
End If
End Function