More efficient code? Moving data

Daroh

Board Regular
Joined
Aug 19, 2016
Messages
62
Hello, is there anyway to make this code more efficient? It goes through over 6000 rows of data (people) and I am adding more people to the table. At present, it takes a long time to do it.

Thanks,



VBA Code:
Sub Match_Data_and Replace()



Dim Data As Worksheet

Dim List As Worksheet

Set Data = ThisWorkbook.Sheets("Patient_Data") ' Raw Data

Set List = ThisWorkbook.Sheets("Wrk_List") ' Search Data - Wrk_List



Dim dArray() As String

Dim lArray() As String



ReDim Preserve dArray(1 To Data.Range("A" & Rows.Count).End(xlUp).Row, 1 To 34)

ReDim Preserve lArray(1 To List.Range("A" & Rows.Count).End(xlUp).Row, 1 To 34)



For a = 1 To Data.Range("A" & Rows.Count).End(xlUp).Row

For b = 1 To 34

dArray(a, b) = Data.Cells(a, b)

Next b

Next a



For a = 1 To List.Range("A" & Rows.Count).End(xlUp).Row

For b = 1 To 34

lArray(a, b) = List.Cells(a, b)

Next b

Next a



Dim MRN As String, lName As String

For a = 2 To UBound(lArray)

MRN = lArray(a, 1)

lName = lArray(a, 2)





For b = 2 To UBound(dArray)

If dArray(b, 1) = MRN And dArray(b, 4) = lName Then



dArray(b, 3) = lArray(a, 3) ' f_Name

dArray(b, 4) = lArray(a, 6)

dArray(b, 6) = lArray(a, 7) ' Phone

dArray(b, 7) = lArray(a, 5)

dArray(b, 36) = lArray(a, 13)

dArray(b, 37) = lArray(a, 14)

dArray(b, 38) = lArray(a, 15)

dArray(b, 39) = lArray(a, 16)

dArray(b, 40) = lArray(a, 17)

dArray(b, 41) = lArray(a, 18)





Exit For

End If

Next b

Next a



'Transfer data back

For a = 2 To UBound(dArray)

For b = 2 To 34

Data.Cells(a, b).Value = dArray(a, b)

Next b

Next a



End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Instead of looping through the worksheet cells in column A of both sheets, assign the ranges to an array and then loop through the array. For example, instead of:
VBA Code:
For a = 1 To Data.Range("A" & Rows.Count).End(xlUp).Row
try:
VBA Code:
Dim vData As Variant, r As Long, c As Long
vData = Data.Range("A2", Data.Range("A" & Rows.Count).End(xlUp)).Value
For r = LBound(vData) To UBound(vData)
    For c = LBound(vData, 2) To UBound(vData, 2)
        dArray(r, c) = Data.Cells(r, c)
    Next c
Next r
The "r'" loops through the rows and the "c" loops through the columns. You could the same for the List sheet.
 
Upvote 0
Instead of looping through the worksheet cells in column A of both sheets, assign the ranges to an array and then loop through the array. For example, instead of:
VBA Code:
For a = 1 To Data.Range("A" & Rows.Count).End(xlUp).Row
try:
VBA Code:
Dim vData As Variant, r As Long, c As Long
vData = Data.Range("A2", Data.Range("A" & Rows.Count).End(xlUp)).Value
For r = LBound(vData) To UBound(vData)
    For c = LBound(vData, 2) To UBound(vData, 2)
        dArray(r, c) = Data.Cells(r, c)
    Next c
Next r
The "r'" loops through the rows and the "c" loops through the columns. You could the same for the List sheet.

Thanks for the response, should the code look like the below? to get the same results as before ?

VBA Code:
Sub Replace_Data()

Dim Data As Worksheet, List As Worksheet, vList As Variant, rl As Long, cl As Long, MRN As String, lName As String, vData As Variant, r As Long, c As Long
Set Data = ThisWorkbook.Sheets("Data") ' Raw Data
List = ThisWorkbook.Sheets("List") ' Search Data - Wrk_List

Dim dArray() As String, lArray() As String
ReDim Preserve dArray(1 To Data.Range("A" & Rows.Count).End(xlUp).Row, 1 To 41)
ReDim Preserve lArray(1 To List.Range("A" & Rows.Count).End(xlUp).Row, 1 To 41)

vData = Data.Range("A2", Data.Range("A" & Rows.Count).End(xlUp)).Value
For r = LBound(vData) To UBound(vData)
    For c = LBound(vData, 2) To UBound(vData, 2)
        dArray(r, c) = Data.Cells(r, c)
    Next c
Next r

vList = List.Range("A2", List.Range("A" & Rows.Count).End(xlUp)).Value
For rl = LBound(vList) To UBound(vList)
    For cl = LBound(vList, 2) To UBound(vList, 2)
        lArray(rl, cl) = List.Cells(rl, cl)
    Next cl
Next rl

For a = 2 To UBound(lArray)
MRN = lArray(a, 1)
lName = lArray(a, 2)

'For b = 2 To UBound(dArray)
If dArray(r, 1) = MRN And dArray(r, 4) = lName Then
dArray(r, 3) = lArray(rl, 3) ' f_Name
dArray(r, 4) = lArray(rl, 6)
                Exit For
            End If
        Next rl
    Next r

'Transfer data back
For a = 2 To UBound(dArray)
For b = 2 To 41
Data.Cells(r, c).Value = dArray(rl, cl)
    Next b
        Next a
End Sub
 
Upvote 0
Could you please use the XL2BB addin to post screen shots (not pictures) of your two sheets and explain in detail what you want to do using a few examples from your data.
 
Upvote 0
This is the sheet with all the people entered on it and it has over 6000 so far and it is growing. The 'Date of review' and 'Comments' will be entered on a seprate day and the data should be pulled from sheet("List"). If the MRN is the same on both Sheet("List") and Sheet("Data"). The code is ran and the data from Sheet("List") is transfered to Sheet("Data") in the that the MRN is matched.

Sheet("Data")
Book1
ABCDEFG
1MRN First NameSecond NamePhioneGenderDate Of reviewComments
2123JohnSecondName1111111m
3654PeterSecondName222222f
4789SimonSecondName333333m
53322MarcusSecondName44444f
65588KevinSecondName55555m
76688JoJoSecondName66666f
Data


Sheet("List") is a work list of people to review on a certain day.

Book1
ABCDEFG
1MRN First NameSecond NamePhioneGenderDate of ReviewComments
2123JohnSecondName1on the day of reviewdata will be entered during review and once the day is finished the code should transfer the data to Sheet(Data) on the same row as the matched MRN.
3
List
 
Upvote 0
Thank you for the sheets. If I were to suggest a macro based on the data you posted, most likely it will not work with your actual file. Your code indicates that you have 34 columns of data while the data you posted has only seven columns. In order to get a working macro, I would need sheets that are exactly representative of your actual data. Please don't leave any cells blank unless they are also blank in your actual file. Also, a few more rows of data in the List sheet would be helpful. Could you re-post the two sheets that have data in exactly the same cells, rows and columns as your actual file? Also clarify if the entire row of data should be transferred or only data in certain columns should be transferred. If not the entire row, please indicate which columns.
 
Upvote 0
Could you please use the XL2BB addin to post screen shots (not pictures) of your two sheets and explain in detail what you want to do using a few examples from your data.
Thank you for the sheets. If I were to suggest a macro based on the data you posted, most likely it will not work with your actual file. Your code indicates that you have 34 columns of data while the data you posted has only seven columns. In order to get a working macro, I would need sheets that are exactly representative of your actual data. Please don't leave any cells blank unless they are also blank in your actual file. Also, a few more rows of data in the List sheet would be helpful. Could you re-post the two sheets that have data in exactly the same cells, rows and columns as your actual file? Also clarify if the entire row of data should be transferred or only data in certain columns should be transferred. If not the entire row, please indicate which columns.

It should be only certain columns. It would be brilliant if the data was entered in the headers if they matched. For example, Date Range (New) would be entered from Sheet("List") to Sheet("Data") if the MRN matched the data would be copied to the correct column header "Date Range (New)".

Sheet("Data") this is the complete list of people and raw data.

CPAP MrExcel.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAM
1MRNLast NameFirst NameGenderAddressContact NumberDOBReferralHeight mWeight KgBMIEp ScoreStop/BangDriver for WorkAHIPLM indexArousal IndexRDINotes: Cancelled tests/ Contacted for cancellationDate of testDate of repeat testDate of CPAP Review ClinicMedical CardProviderModelTreatment ModePressureMask TypeCompliantAdherentMedical CardDate Range (New)AHI (New)%Data USED (NEW)% Days Used 4>hrs (NEW)Average hrs of use (NEW)ESS (NEW)GPGP Address
2123Last_Name1First_NamefAdress_112345678901/01/2024Dr 11.6198.237.8825no65376525.01.24This Column might be empty or have textyesThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayGP 1GP_Address_1
3456Last_Name2First_NamemAdress_212345678902/01/2024Dr 21.738327.7326Yes32586525.01.25noDirect MedicalGP 2GP_Address_2
4789Last_Name3First_NamemAdress_312345678903/01/2024Dr 11.8110727No772387725.01.26yesDirect MedicalYesGP 3GP_Address_3
5321Last_Name4First_NamemAdress_412345678904/01/2024Dr 21.738127.0623n143151525.01.2716/01/2018noDevilbiss Sleep Cubefixed7.5cmH2OnasalynGP 4GP_Address_4
6654Last_Name5First_NamemAdress_512345678905/01/2024Dr 11.69113.739.8122n132471325.01.28yesDirect MedicalNoGP 5GP_Address_5
7987Last_Name6First_NamemAdress_612345678906/01/2024Dr 21.8413139.0026no27689125.01.29noDirect MedicalGP 6GP_Address_6
87788Last_Name7First_NamemAdress_712345678907/01/2024Dr 11.8110632.4026n4615404625.01.30yesYesGP 7GP_Address_7
99988Last_Name8First_NameFAdress_812345678908/01/2024Dr 21.716026no9816429825.01.3119/05/2022noDirect MedicalE20Aauto4-20cmH2OnasalGP 8GP_Address_8
1011225Last_Name9First_NamefAdress_912345678909/01/2024Dr 11.6710738.3726yes15510225.01.3210/01/2023yesDirect MedicalNoGP 9GP_Address_9
Data
Cell Formulas
RangeFormula
K10,K5:K6,K2:K3K2=(J2/(I2*I2))


Sheet("List"), this is the work list for a given day.

CPAP MrExcel.xlsx
GHIJKLMNOPQRSTUVW
1PhoneAHI (ORG)Arousal Index (ORG)PLM index (ORG)ESS (ORG)Date Range (New)AHI (New)%Data USED (NEW)% Days Used 4>hrs (NEW)Average hrs of use (NEW)ESS (NEW)Medical Card:CommentGP AddressGP NameCommerical DriverMedical Card
2123456789656537Entered on dayEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayyesEntered on DayGP 1GP_Address_1no
List
 
Upvote 0
It should be only certain columns. It would be brilliant if the data was entered in the headers if they matched. For example, Date Range (New) would be entered from Sheet("List") to Sheet("Data") if the MRN matched the data would be copied to the correct column header "Date Range (New)".

Sheet("Data") this is the complete list of people and raw data.

CPAP MrExcel.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAM
1MRNLast NameFirst NameGenderAddressContact NumberDOBReferralHeight mWeight KgBMIEp ScoreStop/BangDriver for WorkAHIPLM indexArousal IndexRDINotes: Cancelled tests/ Contacted for cancellationDate of testDate of repeat testDate of CPAP Review ClinicMedical CardProviderModelTreatment ModePressureMask TypeCompliantAdherentMedical CardDate Range (New)AHI (New)%Data USED (NEW)% Days Used 4>hrs (NEW)Average hrs of use (NEW)ESS (NEW)GPGP Address
2123Last_Name1First_NamefAdress_112345678901/01/2024Dr 11.6198.237.8825no65376525.01.24This Column might be empty or have textyesThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textThis Column might be empty or have textEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayGP 1GP_Address_1
3456Last_Name2First_NamemAdress_212345678902/01/2024Dr 21.738327.7326Yes32586525.01.25noDirect MedicalGP 2GP_Address_2
4789Last_Name3First_NamemAdress_312345678903/01/2024Dr 11.8110727No772387725.01.26yesDirect MedicalYesGP 3GP_Address_3
5321Last_Name4First_NamemAdress_412345678904/01/2024Dr 21.738127.0623n143151525.01.2716/01/2018noDevilbiss Sleep Cubefixed7.5cmH2OnasalynGP 4GP_Address_4
6654Last_Name5First_NamemAdress_512345678905/01/2024Dr 11.69113.739.8122n132471325.01.28yesDirect MedicalNoGP 5GP_Address_5
7987Last_Name6First_NamemAdress_612345678906/01/2024Dr 21.8413139.0026no27689125.01.29noDirect MedicalGP 6GP_Address_6
87788Last_Name7First_NamemAdress_712345678907/01/2024Dr 11.8110632.4026n4615404625.01.30yesYesGP 7GP_Address_7
99988Last_Name8First_NameFAdress_812345678908/01/2024Dr 21.716026no9816429825.01.3119/05/2022noDirect MedicalE20Aauto4-20cmH2OnasalGP 8GP_Address_8
1011225Last_Name9First_NamefAdress_912345678909/01/2024Dr 11.6710738.3726yes15510225.01.3210/01/2023yesDirect MedicalNoGP 9GP_Address_9
Data
Cell Formulas
RangeFormula
K10,K5:K6,K2:K3K2=(J2/(I2*I2))


Sheet("List"), this is the work list for a given day.

CPAP MrExcel.xlsx
ABCDEFGHIJKLMNOPQRSTUVW
1MRNDate PreformedFirst_NameSecond_NameDOBGenderPhoneAHI (ORG)Arousal Index (ORG)PLM index (ORG)ESS (ORG)Date Range (New)AHI (New)%Data USED (NEW)% Days Used 4>hrs (NEW)Average hrs of use (NEW)ESS (NEW)Medical Card:CommentGP AddressGP NameCommerical DriverMedical Card
212325.01.24First_NameLast_Name201/01/2024f123456789656537Entered on dayEntered on dayEntered on dayEntered on dayEntered on dayEntered on dayyesEntered on DayGP 1GP_Address_1no
3
4
5
6
List
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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