PC Review


Reply
Thread Tools Rate Thread

get sheet names

 
 
J. Freed
Guest
Posts: n/a
 
      8th Jul 2008
I want to set up a VBA routine that will open up a specific Excel sheet, get
the tab names, store them in a table then close. The tab names and number of
tabs can vary so I need to be able to retrieve as many as there are. I will
then use this to import the data in each of the tabs. Any ideas? TIA....
 
Reply With Quote
 
 
 
 
fredg
Guest
Posts: n/a
 
      8th Jul 2008
On Tue, 8 Jul 2008 08:29:01 -0700, J. Freed wrote:

> I want to set up a VBA routine that will open up a specific Excel sheet, get
> the tab names, store them in a table then close. The tab names and number of
> tabs can vary so I need to be able to retrieve as many as there are. I will
> then use this to import the data in each of the tabs. Any ideas? TIA....



Create a new Table.
TableName "tblWorksheetNames"
Field Name "SheetName" Text datatype, Indexed Yes/No Duplicates

Create a new Module.

Click on tools + references
Set a reference to the Microsoft Excel 10.0 Object Library.

Then copy and paste the below code into the module

Public Sub GetWorksheetNames()
On Error GoTo Err_Handler
Dim xlx As Object, xlw As Object
Set xlx = CreateObject("Excel.Application")

Set xlw = xlx.workbooks.Open("C:\MyFolderName\SpreadsheetName.xls", ,
True)

Dim S As Excel.Worksheet

For Each S In xlw.Worksheets
CurrentDb.Execute "Insert Into tblWorksheetnames(SheetName)
Values('" & S.Name & "');", dbFailOnError
Next S

Exit_Sub:
xlw.Close False
Set xlw = Nothing
xlx.Quit
Set xlx = Nothing
Exit Sub
Err_Handler:
If Err = 3022 Then
Resume Next
Else
MsgBox "Error: " & Err.Number & " " & Err.Description
Resume Exit_Sub
End If
End Sub

Run the code. The sheet names will be added to the table. No name will
be duplicated.

--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
 
Reply With Quote
 
J. Freed
Guest
Posts: n/a
 
      8th Jul 2008
Thanks. Came up with a slightly different way to do it (where tab_name is the
destination table for the tab names):

Function refresh()
Dim aa As Object, ab As Object
DoCmd.SetWarnings 0
DoCmd.OpenQuery "delete_tab_name"
Set aa = CreateObject("excel.application")
Set tabs = CurrentDb.OpenRecordset("tab_name")
filename =<name of spreadsheet>
Set ab = aa.workbooks.Open(filename, , True)
numsheets = aa.sheets.Count
k = 1
Do While k <= numsheets
sheetname = ab.sheets(k).Name
tabs.AddNew
tabs!tab_name = sheetname
tabs.Update
k = k + 1
Loop
ab.Close
End Function

This give me a count then I set up the loop.

"fredg" wrote:

> On Tue, 8 Jul 2008 08:29:01 -0700, J. Freed wrote:
>
> > I want to set up a VBA routine that will open up a specific Excel sheet, get
> > the tab names, store them in a table then close. The tab names and number of
> > tabs can vary so I need to be able to retrieve as many as there are. I will
> > then use this to import the data in each of the tabs. Any ideas? TIA....

>
>
> Create a new Table.
> TableName "tblWorksheetNames"
> Field Name "SheetName" Text datatype, Indexed Yes/No Duplicates
>
> Create a new Module.
>
> Click on tools + references
> Set a reference to the Microsoft Excel 10.0 Object Library.
>
> Then copy and paste the below code into the module
>
> Public Sub GetWorksheetNames()
> On Error GoTo Err_Handler
> Dim xlx As Object, xlw As Object
> Set xlx = CreateObject("Excel.Application")
>
> Set xlw = xlx.workbooks.Open("C:\MyFolderName\SpreadsheetName.xls", ,
> True)
>
> Dim S As Excel.Worksheet
>
> For Each S In xlw.Worksheets
> CurrentDb.Execute "Insert Into tblWorksheetnames(SheetName)
> Values('" & S.Name & "');", dbFailOnError
> Next S
>
> Exit_Sub:
> xlw.Close False
> Set xlw = Nothing
> xlx.Quit
> Set xlx = Nothing
> Exit Sub
> Err_Handler:
> If Err = 3022 Then
> Resume Next
> Else
> MsgBox "Error: " & Err.Number & " " & Err.Description
> Resume Exit_Sub
> End If
> End Sub
>
> Run the code. The sheet names will be added to the table. No name will
> be duplicated.
>
> --
> Fred
> Please respond only to this newsgroup.
> I do not reply to personal e-mail
>

 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      9th Jul 2008
hi,

fredg wrote:
> Click on tools + references
> Set a reference to the Microsoft Excel 10.0 Object Library.
> Dim xlx As Object, xlw As Object
> Set xlx = CreateObject("Excel.Application")
> Dim S As Excel.Worksheet

You shouldn't mix early binding with late binding. Choose one strategy.
I use in the development phase early binding with references. When I
deploy a database the references are removed.


mfG
--> stefan <--
 
Reply With Quote
 
Stefan Hoffmann
Guest
Posts: n/a
 
      9th Jul 2008
hi J.,

J. Freed wrote:
> Function refresh()
> Dim aa As Object, ab As Object
> Set aa = CreateObject("excel.application")
> Set tabs = CurrentDb.OpenRecordset("tab_name")
> Set ab = aa.workbooks.Open(filename, , True)
> Do While k <= numsheets
> Loop
> ab.Close
> End Function

Always close the objects you use. Your code may leave open Excel
instances running, check it with the Task Manager.

In your case:

tabs.Close
Set tabs = Nothing

ab.Close False
Set ab = Nothing

aa.Quit
Set aa = Nothing


mfG
--> stefan <--
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate sheet names from list, assign data to summary sheet. Jason.Alden.Benoit@gmail.com Microsoft Excel Programming 4 20th Jun 2007 10:17 PM
Using Sheet names & Workbook names in VBA coding Colin Foster Microsoft Excel Programming 5 7th Jul 2006 08:04 PM
Retrieving Excel Sheet Names/Column Names Rohit Microsoft ADO .NET 6 6th Dec 2005 02:59 PM
return all worksheet tab names and chart sheet tab names in report - an example DataFreakFromUtah Microsoft Excel Programming 2 6th Oct 2004 09:09 PM
Automatically entering sheet names into a range of cells on one sheet u539771 Microsoft Excel Worksheet Functions 2 23rd Jun 2004 01:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:04 AM.