Sub MatchA_ReturnK()
'Matches value in column A with value in column A on another worksheet. _
Returns a value on same row from a specified column. _
If source of data is a separate workbook, it must be open!
Dim cel As Range, cel2 As Range, cel3 As Range, celHome As Range, _
rg As Range, rgA As Range, targ As Range, targ2 As Range
Dim addr As String, addrA As String, frmla As String
Dim vFind As Variant
On Error Resume Next
Set cel = Application.InputBox("Select first cell where you want imported comments to go.", Type:=8)
Set rg = Application.InputBox("Select any cell from column containing these comments.", _
Title:="Use 'Window' menu to select other workbook", Type:=8)
On Error GoTo 0
If cel Is Nothing Then Exit Sub
If rg Is Nothing Then Exit Sub
Set celHome = ActiveCell
Set rg = Intersect(rg.Worksheet.Columns(rg.Column), rg.Worksheet.UsedRange) 'All the cells that might contain notes
Set rgA = rg.Offset(0, 1 - rg.Column) 'Column A on same worksheet as rg
addr = rg.Address(RowAbsolute:=True, ColumnAbsolute:=True, ReferenceStyle:=xlR1C1, External:=True)
addrA = rgA.Address(RowAbsolute:=True, ColumnAbsolute:=True, ReferenceStyle:=xlR1C1, External:=True)
frmla = "=IF(COUNTIF(" & addrA & ",RC1)=0,"""",INDEX(" & addr & ",MATCH(RC1," & addrA & ",0)) & """")"
Set targ = Cells(Rows.Count, 1).End(xlUp) 'Last cell in column A containing data
Set targ = Range(cel, Cells(targ.Row, cel.Column)) 'All the cells in the target range
targ.NumberFormat = "General"
targ.FormulaR1C1 = frmla 'Put the lookup formulas in the cells of targ
Set targ2 = targ.SpecialCells(xlCellTypeFormulas, xlErrors)
If Not targ2 Is Nothing Then
For Each cel In targ2.Cells
If cel.Text = "#N/A" Then
vFind = cel.EntireRow.Cells(1, 1).Text
Set cel2 = rgA.Find(vFind, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
If Not cel2 Is Nothing Then
Set cel3 = cel2.EntireRow.Cells(1, rg.Column)
cel.Formula = "=" & cel3.Address(External:=True)
End If
End If
Next
End If
targ.WrapText = True
targ.Copy 'Replace the formulas with the values they return
targ.PasteSpecial xlPasteValues
Application.CutCopyMode = False
Application.Goto celHome
End Sub
|