We have 2 excel files, we want to bring all the data from 1st excel file to 2nd excel file which are not there in 2nd excel file.
1st excel file name : Store Linking File.xlsm
2nd excel file name : Inward.xlsm
The existing code which we have written is taking much time because of the loops.
Please help with a code which will be more efficient than the existing one and takes less time to bring data from 1st file.
Existing code is as follows:
1st excel file name : Store Linking File.xlsm
2nd excel file name : Inward.xlsm
The existing code which we have written is taking much time because of the loops.
Please help with a code which will be more efficient than the existing one and takes less time to bring data from 1st file.
Stores Linking File.xlsm | |||
---|---|---|---|
B | |||
2 | GRN | ||
Sheet1 |
Inward.xlsm | |||
---|---|---|---|
A | |||
3 | 10:26:00 | ||
Sheet1 |
Existing code is as follows:
VBA Code:
'module for updating GR details from Store linking file
Sub update_GRDetails1()
ActiveSheet.Unprotect Password:="123456"
Dim i, w1, w2 As Worksheet
Dim FirstR, LastR, n, founR, x, x2 As Long
Dim GRN1 As String
Dim FounD As Range
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Workbooks.Open "\\bpild6987-01\Linking File\Stores Linking File.xlsm", ReadOnly:=True, UpdateLinks:=True
ActiveWindow.Visible = True
Set w1 = Workbooks("Stores Linking File.xlsm").Sheets("Sheet1")
Set w2 = Workbooks("Inward.xlsm").Sheets("Sheet1")
'Find the last Non Zero Cell in Date Column
x2 = w1.Range("K" & Rows.Count).End(xlUp).Row
Do Until w1.Range("k" & x2) <> 0
x2 = x2 - 1
Loop
FirstR = w2.Columns("B:B").Find(What:="GRN").Row
For x = FirstR + 2 To x2
w1.Activate
GRN1 = w1.Cells(x, 2) & w1.Cells(x, 4) & w1.Range("R" & x).Value
w2.Activate
Set FounD = w2.Columns("AZ:AZ").Find(What:=GRN1)
If FounD Is Nothing Then
w2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Value = w1.Range("B" & x).Value
w2.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Value = w1.Range("C" & x).Value
w2.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0).Value = w1.Range("D" & x).Value
w2.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Value = Mid(w1.Range("D" & x).Value, 3, 4)
w2.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Value = w1.Range("E" & x).Value
w2.Cells(Rows.Count, 7).End(xlUp).Offset(1, 0).Value = w1.Range("F" & x).Value
w2.Cells(Rows.Count, 12).End(xlUp).Offset(1, 0).Value = w1.Range("K" & x).Value
w2.Cells(Rows.Count, 11).End(xlUp).Offset(1, 0).Value = w1.Range("J" & x).Value
w2.Cells(Rows.Count, 10).End(xlUp).Offset(1, 0).FormulaR1C1 = "=RC[-2]* RC[-1]"
w2.Cells(Rows.Count, 15).End(xlUp).Offset(1, 0).Value = w1.Range("N" & x).Value
w2.Cells(Rows.Count, 16).End(xlUp).Offset(1, 0).Value = w1.Range("O" & x).Value
w2.Cells(Rows.Count, 19).End(xlUp).Offset(1, 0).Value = w1.Range("S" & x).Value
w2.Cells(Rows.Count, 2).End(xlUp).Offset(0, 50).Value = w1.Range("b" & x).Value & w1.Range("d" & x).Value & w1.Range("R" & x).Value
End If
Next x
w2.Activate
Workbooks("Stores Linking File.xlsm").Close savechanges:=False
'Delete the rows in which GRN or Quantity is Zero
LastR = w2.Cells(Rows.Count, 7).End(xlUp).Row
For i = LastR To FirstR + 1992 Step -1
If Cells(i, 2).Value = 0 Or Cells(i, 7).Value = 0 Then
Cells(i, 6).EntireRow.Delete
End If
Next i
Range(Range("A1993").End(xlDown).Offset(1, 0), Cells(Cells(Rows.Count, 7).End(xlUp).Row, 52)).Select
Range("A1993:AZ1993").Copy
Selection.PasteSpecial Paste:=xlPasteFormats
ActiveSheet.Protect Password:="123456", AllowFiltering:=True
ActiveWorkbook.Save
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Last edited by a moderator: