ALL,
How can i add flags for filtering in this given my data structure? I would like to add flags for filtering certain records from shtMstr to other sheets.
for example:
After the code checks for Var(RPUID) in shtData and grabs related records from shtMstr
if Column P (date) is before the variable StartDate, flag
if column "I" in shtMstr starts with D20, Flag
Once the item is flagged, send to sheet("D20")
Is this possible?
How can i add flags for filtering in this given my data structure? I would like to add flags for filtering certain records from shtMstr to other sheets.
for example:
After the code checks for Var(RPUID) in shtData and grabs related records from shtMstr
if Column P (date) is before the variable StartDate, flag
if column "I" in shtMstr starts with D20, Flag
Once the item is flagged, send to sheet("D20")
Is this possible?
VBA Code:
Sub GetRPUID_Data()
Dim shtData As Worksheet, shtMstr As Worksheet, shtOut As Worksheet
Dim dataLastRow As Long, mstrLastRow As Long, mstrLastCol As Long
Dim dataRng As Range, mstrRng As Range
Dim dataArr As Variant, mstrArr As Variant, outArr As Variant
Dim dictData As Object, dictKey As String
Dim RPUID As Long
Dim i As Long, iMstr As Long, jCol As Long, iOut As Long
Dim StartDate as String
StartDate = 1/13/2022
Set shtData = Worksheets("Sheet1")
Set shtMstr = Worksheets("Sheet2")
Set shtOut = Worksheets("Sheet3")
With shtData
dataLastRow = .Range("A" & Rows.Count).End(xlUp).row
Set dataRng = .Range(.Cells(2, "A"), .Cells(dataLastRow, "A"))
dataArr = dataRng.Value2
End With
With shtMstr
mstrLastRow = .Range("A" & Rows.Count).End(xlUp).row
mstrLastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
Set mstrRng = .Range(.Cells(4, "V"), .Cells(mstrLastRow, mstrLastCol))
mstrArr = mstrRng.Value2
End With
' Load shtData into Dictionary
Set dictData = CreateObject("Scripting.dictionary")
For i = 1 To UBound(dataArr)
RPUID = dataArr(i, 1)
dictKey = RPUID
If Not dictData.Exists(dictKey) Then
dictData(dictKey) = i ' Actual value not required for this scenario ie "" or empty would work
End If
Next i
' Set up output array
ReDim outArr(1 To UBound(mstrArr, 1), 1 To UBound(mstrArr, 2))
' Loop through master and capture lines found in shtData based on dictionary into output array
For iMstr = 1 To UBound(mstrArr)
RPUID = mstrArr(iMstr, 6)
dictKey = RPUID
If dictData.Exists(dictKey) Then ' Add any additional criteria here
iOut = iOut + 1
For jCol = 1 To UBound(mstrArr, 2)
outArr(iOut, jCol) = mstrArr(iMstr, jCol)
Next jCol
End If
Next iMstr
' Write back output array
With shtOut
.Range("A2").Resize(iOut, UBound(outArr, 2)).Value2 = outArr
shtMstr.Range("A1").Resize(1, UBound(outArr, 2)).Copy Destination:=.Range("A1").Resize(1, UBound(outArr, 2))
.Range("A2").Resize(iOut, UBound(outArr, 2)).EntireColumn.AutoFit
End With
End Sub