The situation I am dealing with is one where there is a table with account numbers in the first column, amounts in the fifth column, and 'F' or 'P' in the seventh column. The account numbers match account numbers located on another workbook in the first column. If, in the seventh column on the table, there is an 'F,' the value should be copied, matched, and pasted on the same row in the second column on the target workbook. If there is a 'P,' the value should be matched and pasted on the same row in the third column on the target workbook. What am I doing incorrectly?
VBA Code:
Private Sub CommandButton2_Click()
Dim Dic As Object, key As Variant, oCell As Range, i&
Dim w1 As Worksheet, w2 As Worksheet
Dim cell As Range
Set Dic = CreateObject("Scripting.Dictionary")
Set w1 = Workbooks("HF Pricing Template1").Sheets("Tables")
Set w2 = Workbooks("Book1").Sheets("Sheet1")
For Each cell In Range("Table3[Price_Type]")
If cell.Value = "F" Then
i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row
For Each oCell In w1.Range("M5:M" & i)
If Not Dic.exists(oCell.Value) Then
Dic.Add oCell.Value, oCell.Offset(, 5).Value
End If
Next
i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row
For Each oCell In w2.Range("A2:A" & i)
For Each key In Dic
If oCell.Value = key Then
oCell.Offset(, 3).Value = Dic(key)
End If
Next
Next
End If
Next
End Sub