gpt4 book ai didi

excel - 在导入 Excel 之前过滤 CSV 文件

转载 作者:行者123 更新时间:2023-12-04 20:47:37 27 4
gpt4 key购买 nike

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

x,y

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

其次,我有一个 Excel 文件,它的第一列中的用户 ID 相似但少得多。我只想导入 Excel 文件中包含的那些用户的 y 值。

有谁知道这是怎么做到的吗?

最佳答案

您可以使用 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

关于excel - 在导入 Excel 之前过滤 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com