在Excel中导入之前筛选CSV文件

在Excel中导入之前筛选CSV文件,excel,csv,Excel,Csv,我想导入一个如下所示的CSV文件(逗号是分隔符): 这里,x表示一个用户ID,y表示我要提取的值 其次,我有一个Excel文件,它的第一列中有类似的用户ID,但数量较少。我只想导入Excel文件中包含的用户的y值 有人知道怎么做吗?假设您的数据中有唯一的用户ID(Excel文件和CSV),我只需在单独的选项卡上将CSV导入Excel,然后使用您需要的ID子集(在Excel文件中)执行一个简单的VLOOKUP(),以获得这些特定的y值 注意:我知道在引入CSV之前,这并不是真的过滤任何内容,但它可

我想导入一个如下所示的CSV文件(逗号是分隔符):

这里,x表示一个用户ID,y表示我要提取的值

其次,我有一个Excel文件,它的第一列中有类似的用户ID,但数量较少。我只想导入Excel文件中包含的用户的y值


有人知道怎么做吗?

假设您的数据中有唯一的用户ID(Excel文件和CSV),我只需在单独的选项卡上将CSV导入Excel,然后使用您需要的ID子集(在Excel文件中)执行一个简单的
VLOOKUP()
,以获得这些特定的y值


注意:我知道在引入CSV之前,这并不是真的过滤任何内容,但它可以相当轻松地完成工作(提取y值)。如果你想让这项任务自动化,那么希望有人有一个更具程序性的答案:)

我会这样做,你检查自己的每个用户ID。更改它,让它为你工作。应该很快

注意:我引用了
Microsoft脚本运行时
,它启用了
字典
文件系统对象
文件
文本流
对象

Sub test()

    Dim i As Long
    Dim dicItems As Dictionary
    Dim fso As FileSystemObject
    Dim oFile As File
    Dim saItems() As String, saReturn() As String
    Dim oStream As TextStream
    Dim vUserID As Variant

    'Get stream of file
    Set fso = New FileSystemObject
    Set oFile = fso.OpenTextFile("YourFile.csv")
    Set oStream = oFile.OpenAsTextStream(ForReading)

    Set dicItems = New Dictionary
    'loop through items that you want extracted and put in dictionary
    vUserID = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value2
    ReDim saReturn(1 To UBound(vUserID))
    For i = 1 To UBound(vUserID)
        dicItems.Add vUserID(i, 1), i
    Next i

    'Loop through stream lines
    Do While Not oStream.AtEndOfStream
        saItems = Split(oStream.ReadLine, ",")
        If dicItems.Exists(saItems(0)) Then
            saReturn(dicItems(saItems(0))) = saItems(1)
        End If
    Loop

    'Return information to your spreadsheet
    Range("B1", Range("B" & UBound(saReturn))) = Application.Transpose(saReturn)

End Sub

您可以使用ADO。大致:

Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim TextInput As String

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''
''This is the ACE connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

TextInput = "[Text;FMT=Delimited;HDR=Yes;IMEX=2;DATABASE=Z:\docs]"

''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT a.ID,a.Data " _
       & "FROM " & TextInput & ".[TestIn.csv] a " _
       & "INNER JOIN [Sheet1$] b ON a.ID=b.ID" _


rs.Open strSQL, cn, 3, 3


''Pick a suitable empty worksheet for the results

Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

我可能应该更清楚:CSV文件非常大;它包含超过650000行。Excel只会从我的头顶导入65k行。我远不是导出,但是
VLOOKUP
可能不会查看未导入的行?可能需要一段时间才能看到650000行。很好。我只能从你的答案中选择这个。
Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim TextInput As String

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used.
''
''This is the ACE connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile _
& ";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

TextInput = "[Text;FMT=Delimited;HDR=Yes;IMEX=2;DATABASE=Z:\docs]"

''Late binding, so no reference is needed
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

strSQL = "SELECT a.ID,a.Data " _
       & "FROM " & TextInput & ".[TestIn.csv] a " _
       & "INNER JOIN [Sheet1$] b ON a.ID=b.ID" _


rs.Open strSQL, cn, 3, 3


''Pick a suitable empty worksheet for the results

Worksheets("Sheet3").Cells(2, 1).CopyFromRecordset rs

''Tidy up
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing