Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi,
I use the folowing code to get information from a closed workbook with ADO. It automatically selects the first sheet. How do I get it to select other sheets? e.g. sheet2 sub test() Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset Dim dbConnectionString As String Dim i As Integer dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & "ReadOnly=1;DBQ=E:\AdoVBA\99budget.xls" Set dbConnection = New ADODB.Connection dbConnection.Open dbConnectionString ' open the database connection Set rs = dbConnection.Execute("[" & "a:a" & "]") Do While Not rs.EOF UserForm1.ListBox1.AddItem rs.Fields.Item(i) rs.MoveNext Loop rs.Close dbConnection.Close Set rs = Nothing Set dbConnection = Nothing End sub Help appreciated, Farmer |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Farmer,
This is the code that I use to read closed workbooks Public Sub GetData() Dim oConn As ADODB.Connection Dim oRS As ADODB.Recordset Dim sFilename As String Dim sConnect As String Dim sSQL As String sFilename = "c:\Mytest\Volker1.xls" sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & sFilename & ";" & _ "Extended Properties=Excel 8.0;" sSQL = "SELECT * FROM [Sheet1$]" Set oRS = New ADODB.Recordset oRS.Open sSQL, sConnect, adOpenForwardOnly, _ adLockReadOnly, adCmdText If Not oRS.EOF Then ActiveSheet.Range("A1").CopyFromRecordset oRS Else MsgBox "No records returned.", vbCritical End If oRS.Close Set oRS = Nothing End Sub Just change the Sheet1$ to your named sheet. -- HTH RP "farmer" wrote in message ... Hi, I use the folowing code to get information from a closed workbook with ADO. It automatically selects the first sheet. How do I get it to select other sheets? e.g. sheet2 sub test() Dim dbConnection As ADODB.Connection, rs As ADODB.Recordset Dim dbConnectionString As String Dim i As Integer dbConnectionString = "DRIVER={Microsoft Excel Driver (*.xls)};" & "ReadOnly=1;DBQ=E:\AdoVBA\99budget.xls" Set dbConnection = New ADODB.Connection dbConnection.Open dbConnectionString ' open the database connection Set rs = dbConnection.Execute("[" & "a:a" & "]") Do While Not rs.EOF UserForm1.ListBox1.AddItem rs.Fields.Item(i) rs.MoveNext Loop rs.Close dbConnection.Close Set rs = Nothing Set dbConnection = Nothing End sub Help appreciated, Farmer |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Hi, Thanks It's just what I wanted. Works great. Greetings, Farmer On Sun, 24 Oct 2004 15:19:58 +0100, "Bob Phillips" wrote: Farmer, This is the code that I use to read closed workbooks Public Sub GetData() Dim oConn As ADODB.Connection Dim oRS As ADODB.Recordset Dim sFilename As String Dim sConnect As String Dim sSQL As String sFilename = "c:\Mytest\Volker1.xls" sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & sFilename & ";" & _ "Extended Properties=Excel 8.0;" sSQL = "SELECT * FROM [Sheet1$]" Set oRS = New ADODB.Recordset oRS.Open sSQL, sConnect, adOpenForwardOnly, _ adLockReadOnly, adCmdText If Not oRS.EOF Then ActiveSheet.Range("A1").CopyFromRecordset oRS Else MsgBox "No records returned.", vbCritical End If oRS.Close Set oRS = Nothing End Sub Just change the Sheet1$ to your named sheet. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Macro to select all sheets | Excel Discussion (Misc queries) | |||
Select Multiple sheets | Excel Discussion (Misc queries) | |||
select a1 on all sheets | Excel Programming | |||
All Sheets Unhide and Select | Excel Programming | |||
select sheets by name - how? | Excel Programming |