I'm using VBA to loop through rows on two worksheets and if they match, copy the row from sheet 2 into sheet 1.
My code is supposed to:
- Opens a second workbook
- Copies all info into original workbook on new sheet
- A loop then goes through column F on original Sheet (450+rows) and finds the activecell on new "data" sheet (9,500+rows), upon finding the same value it copies the entire row and pastes it into original sheet then loop starts again.
Although this does work, I'm finding that this takes in excess of 20 minutes, which is way too long! I'm a beginner to VBA and although I've made good progress I'm stuck with this, I've read up on Variants but they're confusing me to be honest! Any help would be appreciated :)
Sub AutoUpdate()
'Opens Enterprise Master Lead File (whichever is present) and auto updates data
' in current sheet depending on if ID ref is present
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'This opens the workbook without setting set date as long as the
'file is always in the same place
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim rng As Range, Cel As Range
Dim sFind As String
Dim lastRow As Long
lastRow = Range("F" & Rows.Count).End(xlUp).Row
Set rng = Range("F2:F" & lastRow)
Set Wb = ThisWorkbook
Set Wb2 = Workbooks.Open("xxxxxxxxxxx.xlsx") 'opens secondary workbook
'Deletes unecessary columns
Range("C:C,D:D,G:G,H:H,I:I,J:J,K:K,M:M,N:N,O:O,P:P,Q:Q,S:S,U:U,V:V,W:W,Z:Z,AD:AD").Select
Selection.Delete Shift:=xlToLeft
Range("A2").Select
Cells.Select
Selection.Copy
Wb.Activate
Sheets.Add.Name = "Data"
Range("A1").Select
ActiveSheet.Paste
Wb2.Close 'closes secondary workbook to speed up process
Wb.Activate
'Loop - finds data in original sheet, finds data in secondary
'sheet, copies new data and pastes, offsets and starts again
Sheets("Corp Leads").Activate
With Wb
rng.Select
'Range("F1").Select
'ActiveCell.Offset(1, 0).Select
'Range(Selection, Selection.End(xlDown)).Select
For Each Cel In rng
If Cel.Value > 0 Then
ActiveCell.Select
sFind = ActiveCell
'Finding matching data
Sheets("Data").Activate
Range("F2").Select
Range(Selection, Selection.End(xlDown)).Select
Cells.Find(What:=sFind, After:= _
ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
'copying new data row
ActiveCell.EntireRow.Select
Selection.Copy
'Finding same data again in original sheet
Sheets("Corp Leads").Activate
Cells.Find(What:=sFind, After:= _
ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
'Pasting new data
ActiveCell.EntireRow.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Finding reference again to offset for loop
Cells.Find(What:=sFind, After:= _
ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.Offset(1, 0).Select
End If
Next Cel
End With
Sheets("Data").Delete
MsgBox ("UPDATED")
End Sub