Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 1st, 2005, 11:15 AM
Registered User
 
Join Date: Jan 2005
Location: , , .
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.

Reply With Quote
  #2 (permalink)  
Old March 3rd, 2005, 12:20 AM
Registered User
 
Join Date: Mar 2005
Location: Greenwood, IN, USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:19 AM.


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