PDA

View Full Version : Excel File-1 copy the value & compare with Excel File-2 and Save it in Excel File-1 ?



UMAN
12-21-2016, 02:29 AM
Hi Iam using excel 2007. I try to copy Unit-price from excel file-2 to the parent excel file-1 while matching the item name & descriptions.
Thanks for the helps & guidance...

My VBA Code



Sub mySales()
Dim LastRow As Integer, i As Integer, erow As Integer, PipeClass As String, Pipe_Desc As String, End_Type As String, PipeSize As String
Dim wbk As Workbook
strFirstFile = "C:\Temp\File1.xlsx"
strSecondFile = "C:\Temp\File2.xlsx"
Set wbk = Workbooks.Open(strFirstFile)
Worksheets("SOR1").Select
LastRow = ActiveSheet.Range(“A” & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
PipeClass = ""
Pipe_Desc = ""
End_Type = ""
PipeSize = ""
PipeClass = ActiveSheet.Cells(i, 1).Value
Pipe_Desc = ActiveSheet.Cells(i, 2).Value
End_Type = ActiveSheet.Cells(i, 3).Value
PipeSize = ActiveSheet.Cells(i, 4).Value
Set wbk = Workbooks.Open(strSecondFile)
Worksheets("SOR2").Select
If Cells(i, 1) = PipeClass And Cells(i, 2) = Pipe_Desc And Cells(i, 3) = End_Type And Cells(i, 4) = PipeSize Then
Range(Cells(i, 12), Cells(i, 12)).Select
Selection.Copy
Worksheets("SOR1").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i
End Sub