D
Dave B
Excel 2000
Hello,
I can't pull all the data I need because the ADO method looks for a header
and if one isn't available, it pulls a blank.
I know what rows I need so I make those the "SourceRange."
I need to get data from an extremely large Excel file (>40,000 rows and thru
HH in columns) without opening it - I'm trying to speed up the process.
Someone suggested I use ADO and now I'm slowly learning it. I am very good
at spreadsheets and comfortable with slightly advanced vb.
Here's the code:
Private Function ReadDataFromWorkbook(SourceFile As String, SourceRange As
String) As Variant
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
dbConnectionString = "DRIVER={Microsoft Excel Driver
(*.xls)};ReadOnly=1;DBQ=" & SourceFile & ";HDR=false"
Set dbConnection = New ADODB.Connection
dbConnection.Open dbConnectionString ' open the database connection
Set rs = dbConnection.Execute("[" & SourceRange & "]") 'The source
range is say A22000:IV25000
ReadDataFromWorkbook = rs.GetRows ' returns a two dim array with all
records in rs
rs.Close
dbConnection.Close ' close the database connection
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Function
InvalidInput:
MsgBox "The source file or source range is invalid!", vbExclamation,
"Get data from closed workbook"
Set rs = Nothing
Set dbConnection = Nothing
End Function
Hello,
I can't pull all the data I need because the ADO method looks for a header
and if one isn't available, it pulls a blank.
I know what rows I need so I make those the "SourceRange."
I need to get data from an extremely large Excel file (>40,000 rows and thru
HH in columns) without opening it - I'm trying to speed up the process.
Someone suggested I use ADO and now I'm slowly learning it. I am very good
at spreadsheets and comfortable with slightly advanced vb.
Here's the code:
Private Function ReadDataFromWorkbook(SourceFile As String, SourceRange As
String) As Variant
Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset
Dim dbConnectionString As String
dbConnectionString = "DRIVER={Microsoft Excel Driver
(*.xls)};ReadOnly=1;DBQ=" & SourceFile & ";HDR=false"
Set dbConnection = New ADODB.Connection
dbConnection.Open dbConnectionString ' open the database connection
Set rs = dbConnection.Execute("[" & SourceRange & "]") 'The source
range is say A22000:IV25000
ReadDataFromWorkbook = rs.GetRows ' returns a two dim array with all
records in rs
rs.Close
dbConnection.Close ' close the database connection
Set rs = Nothing
Set dbConnection = Nothing
On Error GoTo 0
Exit Function
InvalidInput:
MsgBox "The source file or source range is invalid!", vbExclamation,
"Get data from closed workbook"
Set rs = Nothing
Set dbConnection = Nothing
End Function