VBA Nested Loop Help!

Jambi46n2

Active Member
Joined
May 24, 2016
Messages
260
Office Version
  1. 365
Platform
  1. Windows
Hello!
I have a data sheet that is purely Unique with no Duplicates named "Data Library"

TESTING - v1.00 - TEMPLATE - RAW SQL DATA PARSER.xlsm
MNOP
1K_IDAssessmentNameVignetteNameResponseType
2k151371Acute Care AI Critical Thinking AssessmentAbdomen (acute)Action
3k143531Acute Care AI Critical Thinking Assessment (Intermediate)Acute Asthma (Peds)Action
4k140862Acute Care AI Critical Thinking Assessment (Novice)Acute Renal Failure (acute)Actions
5k137744Ambulatory AI Critical Thinking AssessmentAcute Respiratory Distress (Peds)Observation
6k146629Behavioral Health AI Critical Thinking AssessmentAirway Obstruction (perioperative)Problem
7k154554Emergency Care AI Critical Thinking AssessmentAnaphylaxis (amb)Problem
8k147457Intensive Care AI Critical Thinking AssessmentAnxiety (behavioral)
9k137746Labor & Delivery AI Critical Thinking AssessmentArrest of Labor (LD)
10k74382Mother-Baby AI Critical Thinking AssessmentAsthma (emergency)
11k153274NICU AI Critical ThinkingBladder Distention (mb)
12k153819Pediatrics AI Critical Thinking AssessmentBladder Distention (postpartum)
13k152821Perioperative Care AI Critical Thinking AssessmentBonding (mb)
14k152880Postpartum AI Critical Thinking AssessmentBonding (postpartum)
15k142614SNF/LTC AI Critical Thinking AssessmentBreast Engorgement (postpartum)
16k137631Bronchiolitis, Difficulty Breathing (Peds)
17k152587Cerebral Vascular Accident (snf)
18k137177Cerebral Vascular Accident/Stroke (acute)
19k150617Cerebrovascular Accident (acute - intermediate)
20k138496Cerebrovascular Accident (acute - novice)
21k154658Combative Resident (snf)
22k152536Compartment Syndrome (Peds)
23k152223Congestive Heart Failure (critical)
24k137760Congestive Heart Failure (snf)
25k143545Dehydration (snf)
26k137641Dementia/Organic Brain Syndrome (behavioral)
27k152884Denial (behavioral)
28k152641Diabetic Ketoacidosis (acute - intermediate)
29k138008Diabetic Ketoacidosis (acute - novice)
30k137649Diabetic Ketoacidosis (acute)
31k153451Diabetic Ketoacidosis (critical)
32k155791Diabetic Ketoacidosis (emergency)
33k23584Early Patient Discharge (amb)
34k139656Fetal Intolerance of Labor (LD)
35k154197Fire Prevention (perioperative)
36k122235Gastrointestinal Bleeding (emergency)
37k140611Heart Failure (emergency)
38k152636Hematoma (postpartum)
39k137217Hyperactive Labor (LD)
40k137657Hyperbilirubinemia (mb)
41k152942Hyperbilirubinemia (nicu)
42k125266Hyperkalemia (snf)
43k140043Hypertensive Crisis (emergency)
44k153356Hypoglycemia (mb)
45k117030Hypoglycemia (nicu)
46k152727Hypokalemia (Peds)
47k148564Hypotension (LD)
48k153116Hypovolemia / Hemorrhage (critical)
49k153011Increased Intracranial Pressure (acute)
50k154105Increased Intracranial Pressure (critical)
51k137689Increased Intracranial Pressure (emergency)
52k137818Intimate Partner Violence (amb)
53k145243Intraventricular Hemorrhage (nicu)
54k153162Malignant Hyperthermia (perioperative)
55k137272Manic Behavior (behavioral)
56k154066Medication Reconciliation (amb)
57k137821Myocardial Infarction (acute - intermediate)
58k140324Myocardial Infarction (acute - novice)
59k152323Myocardial Infarction (acute)
60k124635Myocardial Infarction (critical)
61k152764Myocardial Infarction (emergency)
62k154463Necrotizing Enterocolitis/NEC (nicu)
63k143536Paranoia or Schizophrenia (behavioral)
64k152456Patent Ductus Arteriosis (nicu)
65k153935Peritonitis (acute - intermediate)
66k93162Peritonitis (critical)
67k152816Pneumonia (emergency)
68k137830Pneumonia (snf)
69k135512Pneumothorax (critical)
70k152767Pneumothorax (nicu)
71k118011Point of Care Lab Error (amb)
72k142601Postpartum Depression (amb)
73Postpartum Hemorrhage (mb)
74Postpartum hemorrhage (postpartum)
75Preeclampsia (LD)
76Premature Rupture of Membranes (LD)
77Preterm Labor (LD)
78Psychosis (behavioral)
79Pulmonary Embolism (acute - intermediate)
80Pulmonary Embolism (acute - novice)
81Pulmonary Embolism (snf)
82Pulmonary Embolism or Pneumothorax (acute)
83R/O Drug, ETOH or Psychosis (behavioral)
84Reprocessing Surgical Instruments Issue (amb)
85Respiratory Distress Syndrome (nicu)
86Retained Item (perioperative)
87Sepsis (critical)
88Sepsis (mb)
89Sepsis (nicu)
90Sepsis (Peds)
91Severe Hemorrhage (perioperative)
92Specimen Error (perioperative)
93Subdural Hematoma (Peds)
94Substance Withdrawal (postpartum)
95Suicidal (Severe Depression)
96Suicide Attempt (Peds)
97Sundown Syndrome (snf)
98Surgical Site Infection (perioperative)
99Suspected Triple I (LD)
100Thrombophlebitis (mb)
101Thrombophlebitis (postpartum)
102Uncontrolled Pain (acute - novice)
103Urinary Retention (acute - intermediate)
104Urinary retention (acute - novice)
105Urinary retention/POUR (acute)
106Urinary Tract Infection (mb)
107Urinary Tract Infection (postpartum)
108Vaccination Contraindicated (amb)
109VignetteName
110Wrong Site Surgery (perioperative)
Data Library


I have another sheet titled "RAW DATA FROM SQL"

TESTING - v1.00 - TEMPLATE - RAW SQL DATA PARSER.xlsm
DEFGHIJKLMNOP
1K_IDAssessmentNameVignetteNameWeightIsUrgentEvaluationNameEvaluationROWIDAbsenceExperienceResponseTypeNamePresenceDescription
2k151371Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ProblemManic Behavior200Manic Behavior
3k143531Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ObservationDelusional or grandiose thoughts200Delusional or grandiose thoughts
4k140862Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3201ObservationLoud, rapid continuous speech0Loud, rapid continuous speech
5k137744Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3201ObservationNervous energy (shaky)0Nervous energy (shaky)
6k146629Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionAnticipate order for drug screen20Anticipate order for drug screen
7k154554Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionAnticipate order for mood stabilizing medications80Anticipate order for mood stabilizing medications
8k147457Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3201ActionAssess for violence towards others0Assess for violence towards others
9k137746Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionGroup therapy (contraindicated)-100Group therapy (contraindicated)
10k74382Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionMonitor patient closely20Monitor patient closely
11k153274Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionNotify provider40Notify provider
12k153819Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3201ActionSocial worker referral or follow-up0Social worker referral or follow-up
13k152821Behavioral Health AI Critical Thinking AssessmentManic Behavior (behavioral)1411400_10 MAN3200ActionTake patient to a quiet room40Take patient to a quiet room
14k152880Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ProblemParanoia or Schizophrenia200Paranoia or Schizophrenia
15k142614Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ObservationFearful50Fearful
16k137631Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ObservationUnpredictable behavior150Unpredictable behavior
17k152587Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3301ActionAnticipate order for labs0Anticipate order for labs
18k137177Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3301ActionAssess hygiene0Assess hygiene
19k150617Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionImmediately restrain patient (contraindicated)-100Immediately restrain patient (contraindicated)
20k138496Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionMaintain safe distance from patient50Maintain safe distance from patient
21k154658Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionOffer patient coffee, food and a place to rest40Offer patient coffee, food and a place to rest
22k152536Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3301ActionProvide female caregiver0Provide female caregiver
23k152223Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionReassure patient they are in a safe place50Reassure patient they are in a safe place
24k137760Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionSearch patient belongings80Search patient belongings
25k143545Behavioral Health AI Critical Thinking AssessmentParanoia or Schizophrenia (behavioral)1211400_11 PARA3300ActionSocial worker referral or follow-up20Social worker referral or follow-up
26k137641Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ProblemR/O Drug, ETOH or Psychosis200R/O Drug, ETOH or Psychosis
27k152884Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ObservationErratic behavior60Erratic behavior
28k152641Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ObservationETOH Withdrawal80ETOH Withdrawal
29k138008Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ObservationPatient history of alcohol abuse60ETOH abuse hx
30k137649Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionAnticipate order for drug screen20Anticipate order for drug screen
31k153451Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionAssess potential for harm20Assess potential for harm
32k155791Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionAssess withdrawal symptoms20Assess withdrawal symptoms
33k23584Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionCheck room for contraband10Check room for contraband
34k139656Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionComplete mental health assessment40Complete mental health assessment
35k154197Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionEvaluate need for additional medication40Evaluate medications
36k122235Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionGroup therapy (contraindicated)-100Group therapy (contraindicated)
37k140611Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionNotify provider40Notify provider
38k152636Behavioral Health AI Critical Thinking AssessmentR/O Drug, ETOH or Psychosis (behavioral)1211400_2 ETOH2700ActionReorient to reality10Reorient to reality
39k137217Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ProblemDenial200Denial
40k137657Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ObservationPatient did not attend group therapy100Patient did not attend group therapy
41k152942Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ObservationPatient focused on another patient100Patient focused on another patient
42k125266Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ActionExplore reason for therapy avoidance20Explore reason for therapy avoidance
43k140043Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ActionFollow-up on group attendance20Follow-up on group attendance
44k153356Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ActionRedirect patient focus100Redirect patient focus
45k117030Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ActionRemind patient of therapy schedule20Remind patient of therapy schedule
46k152727Behavioral Health AI Critical Thinking AssessmentDenial (behavioral)1201400_3 DEN2800ActionReview therapy expectations40Review therapy expectations
47k148564Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ProblemPsychosis200Psychosis
48k153116Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ObservationAgitation100Agitation
49k153011Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ObservationDelusional or grandiose thoughts100Delusional or grandiose thoughts
50k154105Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionAcknowledge patient fear0Acknowledge patient fear
51k137689Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionAnticipate order for antipsychotic meds40Anticipate order for antipsychotic meds
52k137818Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionAnticipate order for drug screen0Anticipate order for drug screen
53k145243Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionDo not challenge voices0Do not challenge voices
54k153162Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionEvaluate ability for ADL0Evaluate ability for activities of daiily living
55k137272Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionExplore "bad thoughts" and what voices are saying40Explore "bad thoughts" and what they are saying
56k154066Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionMaintain consistent staff0Maintain consistent staff
57k137821Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionMaintain safe distance from patient40Maintain safe distance from patient
58k140324Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionMonitor effectiveness of medications0Monitor effectiveness of medications
59k152323Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionNo roommate0No roommate
60k124635Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionNotify provider20Notify provider
61k152764Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionPlace patient in a monitored room0Place patient in a monitored room
62k154463Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionReassure patient they are in a safe place20Reassure patient they are in a safe place
63k143536Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2901ActionReduce stimuli0Reduce stimuli
64k152456Behavioral Health AI Critical Thinking AssessmentPsychosis (behavioral)1211400_4 PSY2900ActionReorient to reality20Reorient to reality
65k153935Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ProblemDementia or Alzheimer's200Organic Brain Syndrome
66k93162Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ObservationAgitation40Agitation
67k152816Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ObservationPatient history of changes in behavior60Patient history of changes in behavior
68k137830Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ObservationPatient is uncommunicative100Patient is uncommunicative
69k135512Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ActionAnticipate order for medications20Anticipate order for medications
70k152767Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3001ActionAsk husband to bring familiar items from home0Ask husband to bring familiar items from home
71k118011Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ActionAttempt to engage patient in conversation20Attempt to engage patient in conversation
72k142601Behavioral Health AI Critical Thinking AssessmentDementia/Organic Brain Syndrome (behavioral)1201400_5 DEM3000ActionComplete physical assessment (PIPHA)0Complete physical assessment (PIPHA)
RAW DATA FROM SQL

In a new Sheet titled "Scrubbed Output" I need every instance where K_ID is found in the "RAW DATA FROM SQL" where there are duplicates, and Output all the headers where there are duplicates that pertain to each K_ID., where the "Data Library" Sheet has unique values. So every instance where K_ID, AssessmentName, VignetteName, and ResponseType are found. The "Scrubbed Output" sheet should have all columns from "RAW DATA FROM SQL". Does this make sense? Thank you so much in advance. Have an urgent situation to handle, and dont have SQL access.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code I've tried using but not working out. Just pulling in headers and empty on the output.

Here is More context:

Inside this Excel File I have important Columns on two different sheets.
I will need code written in VBA to complete this task.

Sheet 1 is named "RAW DATA FROM SQL"
Here are the names of relevant columns in row 1:

D1 is named "K_ID"
E1 is named "AssessmentName"
F1 is named "VignetteName"
G1 is named "Weight"
H1 is named "IsUrgent"
I1 is named "EvaluationName"
J1 is named "EvaluationROWID"
K1 is named "Absence"
L1 is named "Experience"
M1 is named "ResponseType"
N1 is named "Name"
O1 is named "Presence"
P1 is named "Description"

These columns may contain duplicate information which is good.

Sheet 2 is named "Data Library"
Here are the names of relevant columns in row 1:

M1 is named "K_ID"
O1 is named "AssessmentName"
P1 is named "VignetteName"
Q1 is named "ResponseType"

The columns listed from "Data Library" do not have duplicate information.

For Each unique value in "K_ID" in on "Data Library"
Starting in Row 2
Search through every instance of "AssessmentName", "VignetteName", "ResponseType" from "Data Library"
To extract all data from "RAW DATA FROM SQL" starting in Row 2, to retrieve data from the list provided in "Data Library"

Populate these results into sheet named "Scrubbed Output" with a timestamp

Can you please help me with this task?

Thank you in advance.


VBA Code:
Sub ScrubData()

' Declare variables for the worksheets
Dim rawDataWS As Worksheet
Dim dataLibraryWS As Worksheet
Dim scrubbedOutputWS As Worksheet

' Set the worksheets to the appropriate sheet
Set rawDataWS = ThisWorkbook.Sheets("RAW DATA FROM SQL")
Set dataLibraryWS = ThisWorkbook.Sheets("Data Library")
Set scrubbedOutputWS = ThisWorkbook.Sheets("Scrubbed Output")

' Clear the contents of the "Scrubbed Output" sheet
scrubbedOutputWS.Cells.Clear

' Add a timestamp to the "Scrubbed Output" sheet
scrubbedOutputWS.Cells(1, 1) = "Scrubbed on " & Format(Now(), "MM/DD/YYYY HH:MM:SS")

' Declare variables for the columns in each sheet
Dim currentKID As String
Dim rawDataKID As Integer
Dim rawDataAssessmentName As Integer
Dim rawDataVignetteName As Integer
Dim rawDataWeight As Integer
Dim rawDataIsUrgent As Integer
Dim rawDataEvaluationName As Integer
Dim rawDataEvaluationROWID As Integer
Dim rawDataAbsence As Integer
Dim rawDataExperience As Integer
Dim rawDataResponseType As Integer
Dim rawDataName As Integer
Dim rawDataPresence As Integer
Dim rawDataDescription As Integer

Dim dataLibraryKID As Integer
Dim dataLibraryAssessmentName As Integer
Dim dataLibraryVignetteName As Integer
Dim dataLibraryResponseType As Integer

' Set the variables to the appropriate column numbers
rawDataKID = 4
rawDataAssessmentName = 5
rawDataVignetteName = 6
rawDataWeight = 7
rawDataIsUrgent = 8
rawDataEvaluationName = 9
rawDataEvaluationROWID = 10
rawDataAbsence = 11
rawDataExperience = 12
rawDataResponseType = 13
rawDataName = 14
rawDataPresence = 15
rawDataDescription = 16

dataLibraryKID = 13
dataLibraryAssessmentName = 15
dataLibraryVignetteName = 16
dataLibraryResponseType = 17

' Declare variables for the last row and column in each sheet
Dim rawDataLastRow As Integer
Dim dataLibraryLastRow As Integer

' Set the variables to the last row in each sheet
rawDataLastRow = rawDataWS.Cells(rawDataWS.Rows.Count, "A").End(xlUp).Row
dataLibraryLastRow = dataLibraryWS.Cells(dataLibraryWS.Rows.Count, "A").End(xlUp).Row

' Declare a variable to keep track of the next empty row in the "Scrubbed Output" sheet
Dim nextEmptyRow As Integer
nextEmptyRow = 2

Dim col As Variant

' Copy the headers from the "RAW DATA FROM SQL" sheet to the "Scrubbed Output" sheet
For col = 1 To rawDataWS.Cells(1, rawDataWS.Columns.Count).End(xlToLeft).Column
    scrubbedOutputWS.Cells(1, col).Value = rawDataWS.Cells(1, col).Value
Next col

Dim i As Long
' Loop through each unique value in the "K_ID" column of the "Data Library" sheet
For i = 2 To dataLibraryLastRow
If IsEmpty(dataLibraryWS.Cells(i, dataLibraryKID)) = False Then
' Declare a variable to store the current K_ID value

currentKID = dataLibraryWS.Cells(i, dataLibraryKID).Value

Dim j As Long

    ' Loop through each row in the "RAW DATA FROM SQL" sheet
    For j = 2 To rawDataLastRow
        ' Check if the K_ID value in the current row of the "RAW DATA FROM SQL" sheet matches the current K_ID value
        If rawDataWS.Cells(j, rawDataKID).Value = currentKID Then
            ' Copy the relevant data from the "RAW DATA FROM SQL" sheet to the "Scrubbed Output" sheet
            scrubbedOutputWS.Cells(nextEmptyRow, 1) = rawDataWS.Cells(j, rawDataAssessmentName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 2) = rawDataWS.Cells(j, rawDataVignetteName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 3) = rawDataWS.Cells(j, rawDataWeight).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 4) = rawDataWS.Cells(j, rawDataIsUrgent).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 5) = rawDataWS.Cells(j, rawDataEvaluationName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 6) = rawDataWS.Cells(j, rawDataEvaluationROWID).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 7) = rawDataWS.Cells(j, rawDataAbsence).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 8) = rawDataWS.Cells(j, rawDataExperience).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 9) = rawDataWS.Cells(j, rawDataResponseType).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 10) = rawDataWS.Cells(j, rawDataName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 11) = rawDataWS.Cells(j, rawDataPresence).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 12) = rawDataWS.Cells(j, rawDataDescription).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 13) = dataLibraryWS.Cells(i, dataLibraryAssessmentName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 14) = dataLibraryWS.Cells(i, dataLibraryVignetteName).Value
            scrubbedOutputWS.Cells(nextEmptyRow, 15) = dataLibraryWS.Cells(i, dataLibraryResponseType).Value
         
            ' Increase the value of nextEmptyRow by 1
            nextEmptyRow = nextEmptyRow + 1
        End If
    Next j
End If

Next i

' copy all headers from "RAW DATA FROM SQL" sheet to "Scrubbed Output" sheet
For col = 1 To rawDataWS.UsedRange.Columns.Count

scrubbedOutputWS.Cells(1, col).Value = rawDataWS.Cells(1, col).Value
Next col

' Loop through each unique value in the "K_ID" column of the "Data Library" sheet
For i = 2 To dataLibraryLastRow
If IsEmpty(dataLibraryWS.Cells(i, dataLibraryKID)) = False Then
' Declare a variable to store the current K_ID value

currentKID = dataLibraryWS.Cells(i, dataLibraryKID).Value

    ' Loop through each row in the "RAW DATA FROM SQL" sheet
    For j = 2 To rawDataLastRow
        If rawDataWS.Cells(j, rawDataKID).Value = currentKID Then
            ' Copy the data to the "Scrubbed Output" sheet
            For col = 1 To rawDataWS.UsedRange.Columns.Count
                scrubbedOutputWS.Cells(nextEmptyRow, col).Value = rawDataWS.Cells(j, col).Value
            Next col
            nextEmptyRow = nextEmptyRow + 1
        End If
    Next j
End If
Next i

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top