简体   繁体   中英

Run-time error ‘1004’ – for VLOOKUP

I'm trying to build some VBA code with double VlookUp but I get the run-time error '1004': Application-defined or object-defined error. The goal of this is:

I receive a .csv file from customers with data: Login , Name eMail , Card Number , Host Login , etc. I load the .csv file to the worksheet "Data" and run a vlookup to copy the data to worksheet "users". As customers never build the .csv file with the same order I can't create the vlookup with a fixed column number to copy to the worksheet "users". The code I'm using:

Sub browseFileTest()
Dim desPathName As Variant
Dim DestCell As Range
Dim iemail As Integer
Dim PosEmail As Integer
Dim icard As Integer
Dim Poscard As Integer
Dim ihost As Integer
Dim Poshost As Integer
Dim iemailD As Integer
Dim PosEmailD As Integer
Dim icardD As Integer
Dim PoscardD As Integer
Dim ihostD As Integer
Dim PoshostD As Integer

'Import file to worksheet Data
    desPathName = Application.GetOpenFilename(fileFilter:="Excel Files (*.*), *.*", Title:="Please select a file")
    If desPathName = False Then
        MsgBox "Stopping because you did not select a file. Reselect a destination file through the menu"
        Exit Sub
    Else
    With Sheets("Data").QueryTables.Add(Connection:= _
         "TEXT;" & desPathName, Destination:=Sheets("Data").Range("$A$1"))
        .Name = "users"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
'Find cells position to 1º Vlookup
         For iemail = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, iemail), "Email") Then
          PosEmail = iemail - 1
         End If
         Next
         For icard = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, icard), "CardNumber") Then
         Poscard = icard - 1
         End If
         Next
         For ihost = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, ihost), "HostLogin") Then
         Poshost = ihost - 1
         End If
         Next
    Sheets("Data").Select
' Find cells position to 2ª Vlookup
         For iemailD = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, iemailD), "Email") Then
         PosEmailD = iemailD - 1
         End If
         Next
         For icardD = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, icardD), "CardNumber") Then
         PoscardD = icardD - 1
         End If
         Next
         For ihostD = 1 To Cells(1, 1).End(xlToRight).Column
         If InStr(Cells(1, ihostD), "HostLogin") Then
         PoshostD = ihostD - 1
         End If
         Next
' Copy cells from Worksheet Data to WorkSheet Users
    **With Sheets("Users").Range("A2", Sheets("Users").Cells(Rows.Count, "A").End(xlUp))
        .Offset(, PosEmail).Formula = "=VLOOKUP(A" & .Row & ",'Data'!$A:$I,(,""" & PosEmailD & """ ),FALSE)"**
        .Offset(, 1).Value = .Offset(, 1).Value
    End With
    End If
End Sub

Do you think this is possible?

It appears the problem was with the syntax of VLOOKUP, which should be:

VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)

in particular with the construction of the col_index_num parameter. Hence

…:$I," & PosEmailD & ",FA...

rather than

` …:$I,(,""" & PosEmailD & """ ),FA…

seems to have worked.

(Two pairs of double quotes, one pair of parentheses and a comma surplus).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM