Excel 运行时错误‘;1004&x2019–;为VLOOKUP

Excel 运行时错误‘;1004&x2019–;为VLOOKUP,excel,syntax,runtime-error,vlookup,vba,Excel,Syntax,Runtime Error,Vlookup,Vba,我试图用双VlookUp构建一些VBA代码,但我得到了运行时错误“1004”: 应用程序定义或对象定义错误。其目标是: 我从客户处收到一个.csv文件,其中包含以下数据:登录名、姓名电子邮件、卡号、主机登录名,等等。我将.csv文件加载到工作表“数据”中,并运行vlookup将数据复制到工作表“用户”。由于客户从不以相同的顺序构建.csv文件,因此我无法创建具有固定列号的vlookup以复制到工作表“用户”中。我正在使用的代码: Sub browseFileTest() Dim desPathN

我试图用双VlookUp构建一些VBA代码,但我得到了运行时错误“1004”: 应用程序定义或对象定义错误。其目标是:

我从客户处收到一个.csv文件,其中包含以下数据:
登录名
姓名电子邮件
卡号
主机登录名
,等等。我将.csv文件加载到工作表“数据”中,并运行vlookup将数据复制到工作表“用户”。由于客户从不以相同的顺序构建.csv文件,因此我无法创建具有固定列号的vlookup以复制到工作表“用户”中。我正在使用的代码:

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

你认为这是可能的吗?

看来问题在于VLOOKUP的语法,应该是:

VLOOKUP(查找值、表数组、列索引数、范围查找)

尤其是构造
col\u index\u num
参数。因此

…:$I,“&posemail&”,FA

而不是

`…:$I,(,“”“&posemail&“”),FA

似乎已经奏效了


(两对双引号、一对括号和一个逗号多余)。

您考虑过使用SQL查询您的csv文件吗?嗨,Brad,我需要使用它,因为数据进入工作表用户后,有很多工作要做,所以我认为这是实现目标的最佳方法。在所有情况下,非常感谢你的评论,我不是说把它放进数据库或任何东西。您可以在数据位于CSV文件中时使用SQL,也可以将工作表视为“表”,并通过VBA使用SQL进行查询。它可以让数据更具可读性。这是一个起点,我会尝试一下,让我们看看。非常感谢。Brad@Carlos,出现错误时,代码在哪一行?