[英]Retrieving data from Excel spreadsheet to Excel spreadsheet in a block
Here's my problem : I want to retrieve data from an Excel XML spreadsheet (*.xlsx) within another Excel spreadsheet without opening it. 这是我的问题:我想从另一个Excel电子表格中的Excel XML电子表格(* .xlsx)中检索数据而不打开它。 So I gave a chance to OLEDB with the ACE Provider.
所以我给ACEB提供了ACEB的机会。
The connection worked and I made what I wanted, by looping through my recordset. 通过循环我的记录集,连接工作,我做了我想要的。 But now I want some optimization, ie putting my recordset into excel in a block instead of looping through it.
但现在我想要一些优化,即将我的记录集放入块中的excel而不是循环遍历它。
Therefore I made something like this : 因此我做了这样的事情:
Sub RetrieveData()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
With con
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=Path\File.xlsx; _
Extended Properties=""Excel 12.0 Xml;HDR=NO;IMEX=1"""
.Open
Set rs = .Execute("Select * From [Sheet1$]")
'Problem here
Range(Cells(1, 1), Cells(rs.RecordCount, rs.Fields.Count - 1)) = rs
.Close
End With
Exit Sub
The thing here, is that I'm currently dealing with technologies which I don't know much about them and can't find any documentation on them (eg Microsoft ACE 12.0 Provider for OLEDB). 这里的问题是,我目前正在处理我不太了解它们的技术,并且找不到任何关于它们的文档(例如,用于OLEDB的Microsoft ACE 12.0 Provider)。
Regards. 问候。
(And don't even hesitate to correct my poor grammar) (甚至不要犹豫纠正我糟糕的语法)
I think this is what you need: 我想这就是你需要的:
Cells(1,1).CopyFromRecordset rs
Quite simple, don't you think. 很简单,你不觉得。 But put it instead of this line:
但是把它换成这一行:
Range(Cells(1,1)................ = rs
And remember to remove comment: 'Problem here
:) 并记得删除评论:
'Problem here
:)
By the way, data you get in your sheet don't include columns heading. 顺便说一下,您在工作表中获得的数据不包括列标题。 But I hope you'll cope with that separately.
但我希望你能单独应对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.