Macro for last post, and also for anser to this Thread post:
https://excelfox.com/forum/showthrea...ll=1#post14578
https://excelfox.com/forum/showthrea...ll=1#post14591
https://www.excelforum.com/excel-pro...n-matches.html
https://excelfox.com/forum/showthrea...ll=1#post14588
https://eileenslounge.com/viewtopic.php?f=30&t=34936
Code:
' https://excelfox.com/forum/showthread.php/2349-Copy-and-Paste-based-on-comparisons-Match-and-calculations-of-cells-in-two-workbooks/page4#post14578 https://excelfox.com/forum/showthread.php/2349-Copy-and-Paste-based-on-comparisons-Match-and-calculations-of-cells-in-two-workbooks?p=14578&viewfull=1#post14578
' https://excelfox.com/forum/showthread.php/2349-Copy-and-Paste-based-on-comparisons-Match-and-calculations-of-cells-in-two-workbooks?p=14591&viewfull=1#post14591
Sub STEP6()
Dim Ws1 As Worksheet, Ws2 As Worksheet
Dim Wb1 As Workbook, Wb2 As Workbook
Dim R2 As Long, Lr As Long, I As Long ' r2&, lr&, i&
Set Wb1 = Workbooks("1.xls") ' For open workbook Alternatively to open worknok - Examples: Workbooks.Open(ThisWorkbook.Path & "\1.xls") ' Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\1.xls")
Set Ws1 = Wb1.Worksheets.Item(1)
Set Wb2 = Workbooks("AlertCodes.xlsx") ' Workbooks.Open(ThisWorkbook.Path & "\AlertCodes.xlsx") ' Workbooks.Open("C:\Users\WolfieeeStyle\Desktop\Files\AlertCodes.xlsx")
Set Ws2 = Wb2.Worksheets.Item(4)
With Ws1
Let Lr = .Cells(.Rows.Count, "A").End(xlUp).Row
For I = 2 To Lr
' Reset r2
R2 = 0
' Avoid error messages
On Error Resume Next
' Try to get r2
R2 = WorksheetFunction.Match(.Cells(I, "I"), Ws2.[B:B], 0) ' R2 returns the matched row if there is a match
' Restore error handling
On Error GoTo 0
' Only set column K if r2 is valid
If R2 > 0 Then
If Ws2.Cells(R2, "D") = ">" Then
.Cells(I, "K").Value = Ws2.Cells(R2, "E").Value - 0.01 * Ws2.Cells(R2, "E").Value ' Ws2.Cells(I, "E").Value - 0.01 * Ws2.Cells(I, "E").Value
Else
.Cells(I, "K").Value = Ws2.Cells(R2, "E").Value + 0.01 * Ws2.Cells(R2, "E").Value ' Ws2.Cells(I, "E").Value + 0.01 * Ws2.Cells(I, "E").Value
End If
End If
Next I
End With
Wb1.Save
Wb1.Close
Wb2.Close
End Sub
Bookmarks