Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2005, 11:15 AM
Registered User
 
Join Date: Jan 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Find Unknown Worksheet Names in an Excel Workbook

I am trying to create an application that can read multiple sheets out of an Excel Workbook. Opening the connection and reading the data out of a known sheet is no problem. I am running into a wall when trying to extract the data from an unknown sheet and multiple sheets. I want to be able to read the data from all of the sheets in a workbook. Here is the code i am currently using:

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & FileLoadName & "; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)

'######################################
'Populate grid on GUI
'######################################
DataGrid1.DataSource = DS.Tables(0).DefaultView

This brings in all of the data on Sheet1, but doesn't display Sheet2 or Sheet3!!! Any help would be appreciated.

 
Old March 3rd, 2005, 12:20 AM
Registered User
 
Join Date: Mar 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to coders4hire
Default

I just asked a similar question for importing data from a workbook. Here was the reply I received:

Sub GetWorksheetNames(strFile as string)
Dim xl As New Excel.Application
Dim wkbk As Excel.Workbook
Dim strFile As String
Dim I as Integer
Dim J as Long

Set wkbk = xl.Workbooks.Open(strFile)

For I = 1 to wkbk.Worksheets.Count
  J = 1
  Do While wkbk.Worksheets(I).Range("A" & J).Value <> ""
    J = J + 1
  Loop
  Debug.Print wkbk.Worksheets(I).Name
  Debug.Print "Number of Records: " & J
  ' Your code here to add these sheet names to a Listbox or something
Next I

wkbk.Close
Set wkbk = Nothing
xl.Quit
Set xl = Nothing

End Sub



Quote:
quote:Originally posted by bcarmen
 I am trying to create an application that can read multiple sheets out of an Excel Workbook. Opening the connection and reading the data out of a known sheet is no problem. I am running into a wall when trying to extract the data from an unknown sheet and multiple sheets. I want to be able to read the data from all of the sheets in a workbook. Here is the code i am currently using:

MyConnection = New System.Data.OleDb.OleDbConnection( _
"provider=Microsoft.Jet.OLEDB.4.0; " & _
"data source=" & FileLoadName & "; " & _
"Extended Properties=Excel 8.0;")
' Select the data from Sheet1 of the workbook.
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _
"select * from [Sheet1$]", MyConnection)

DS = New System.Data.DataSet
MyCommand.Fill(DS)

'######################################
'Populate grid on GUI
'######################################
DataGrid1.DataSource = DS.Tables(0).DefaultView

This brings in all of the data on Sheet1, but doesn't display Sheet2 or Sheet3!!! Any help would be appreciated.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy worksheet to another workbook and rename it Probleminfinity Excel VBA 1 August 19th, 2007 11:38 PM
Copy Worksheet into another workbook money Excel VBA 4 March 8th, 2007 05:17 AM
VLookup, arrays, and worksheet names chp Excel VBA 0 April 7th, 2006 03:15 PM
Copy a worksheet range to a new workbook cej2583 Excel VBA 2 March 14th, 2006 11:55 PM
Opening unknown table names NiMot VB Databases Basics 2 July 17th, 2003 02:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.