我正在尝试打开CSV文件并进行查询,并将结果返回到“ThisWorkbook”第二个工作表的A列。 我没有收到任何错误,所以我不明白为什么不将记录集复制到excel中。
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
Dim currentDataFilePath As String
Dim currentDataFileName As String
Dim nextRow As Integer
currentDataFilePath = "C:\Users\M\folder\"
currentDataFileName = "csv-file"
con.Open "Provider=Microsoft.JET.OLEDB.4.0;" & _
"Data Source=" & currentDataFilePath & ";" & _
"Extended Properties=""text;HDR=NO;FMT=Delimited;IMEX=1"""
'rs.ActiveConnection = con
rs.Open "SELECT Name FROM [" & currentDataFileName & ".csv] WHERE Datatype ='TYPE3'",
con
ThisWorkbook.Worksheets("Sheet2").Range("A:A").CopyFromRecordset rs
rs.Close
con.Close
Set rs = Nothing
Set con = Nothing
End Sub
答案 0 :(得分:2)
您可以参考CopyFromRecordset()
方法。
根据您的上述代码,在rs.Open
命令后添加如下内容:
ActiveWorksheet.Range("A1").CopyFromRecordset rs
在此处查看更多内容:http://msdn.microsoft.com/en-us/library/office/ff839240%28v=office.15%29.aspx