I posted a code on here previously that is somewhat similar to the code below...a fellow on here turned it into an array and literally transformed the code in terms of speed...it went from 12 minutes to 20 seconds. This code here takes about 4 minutes to run as it goes through about 5600 lines of data. Is there a way to speed this up (maybe by using an array instead)?
By the way - OptimizeCode_Begin and End call outs are the typical Application.ScreenUpdating, Application.EnableEvents, etc.
Thanks!
By the way - OptimizeCode_Begin and End call outs are the typical Application.ScreenUpdating, Application.EnableEvents, etc.
Thanks!
Code:
Sub Merge_Data()
'Call OptimizeCode_Begin
Dim cell1 As Range, rng1 As Range, cell2 As Range, rng2 As Range
Sheets("Sheet2").Range("A1:BB1").Copy Destination:=Sheets("Sheet1").Range("L1") 'copy column headers from Sheet 2 to Sheet 1
Set rng1 = Sheets("Sheet1").Range("E2:E" & Sheets("Sheet1").Cells(Sheets("Sheet1").Rows.Count, "E").End(xlUp).Row) 'defining variable rng1 -->Sheet1, column E starting at E2 and going to last value in column E
Set rng2 = Sheets("Sheet2").Range("E2:E" & Sheets("Sheet2").Cells(Sheets("Sheet2").Rows.Count, "E").End(xlUp).Row) 'defining variable rng2 -->Sheet2, column E starting at E2 and going to last value in column E
For Each cell2 In rng2 'for each cell in range 2 defined above (column E in Sheet 2)...
For Each cell1 In rng1 'for each cell in range 1 defined above (column E in Sheet 1)...
If cell2.Value = cell1.Value And cell2.Offset(0, -3) = cell1.Offset(0, -3).Value Then 'if the value of cell2 equals the value of cell1 AND the value of cell2 (offset by 3 columns) equals the value of cell1 (offset by 3 columns) then...
Sheets("Sheet2").Range("A" & cell2.Row & ":BB" & cell2.Row).Copy Destination:=Sheets("Sheet1").Range("L" & cell1.Row & ":BM" & cell1.Row) 'copy corresponding columns A:BB from current row in Sheet 2 and copy into Sheet 1 corresponding columns L:BM into current row
Exit For
End If
Next
Next
'Call OptimizeCode_End
End Sub
Last edited: