Question : VLOOKUP not returning entire contents

I've written the following code to pull data from one workbook to another:

=IF(ISNA(VLOOKUP($A4,'[893060.xls]Sheet1'!$A:$K,11,FALSE)),"", (VLOOKUP($A4,'[893060.xls]Sheet1'!$A:$K,11,FALSE)))

My only problem is, it is not copying the entire contents of the cell from the original workbook.  Is there a character limit using VLOOKUP?  How can I ensure it copies ALL the text from the original?

Thanks as always - I'm sure someone'll have the answer in 5 mins.

Answer : VLOOKUP not returning entire contents

There are two separate issues:
1) If column K in the target row is blank, then the INDEX formula returns a value that Excel interprets as 0. The cure is to concatenate an empty string to the end:
=INDEX(xxx,MATCH(yy, zzzz, 0)) & ""

2) If column A on one worksheet is text that looks like numbers and real numbers on the other worksheet, then MATCH can't find the match--but COUNTIF can. The net result is a #N/A error value. As a workaround, I used the Find method to find the match in those cases.

Brad
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
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
Random Solutions  
 
programming4us programming4us