Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Importing / Linking / November 2004

Tip: Looking for answers? Try searching our database.

Looking for Sheet inside Excel file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mauricio Silva - 26 Oct 2004 19:13 GMT
How can I verify if one specific sheet is inside the spresdsheet?
I need to verify if it exists, otherwise a error message of "Out of range"
appears and it does not make much os a sense to my users.

To open the Excel file I am using:

   Dim xlApp As Object
   Dim MySheet As Object

   Set xlApp = CreateObject("Excel.Application")
   Set MySheet = xlApp.WorkBooks.Open(FileName).Sheets(SheetName.Value)

   (...)
   MySheet.Application.ActiveWorkBook.Close False
   xlApp.Quit
   
   Set MySheet = Nothing
   Set rstDebit = Nothing

Thanks
Ken Snell [MVP] - 26 Oct 2004 19:40 GMT
Try this:

   Dim xlApp As Object
   Dim MySheet As Object
   Dim MyFile As Object
   Dim lngSheet As Long

   Set xlApp = CreateObject("Excel.Application")
   Set MyFile = xlApp.WorkBooks.Open(FileName)

   For lngSheet = 0 To MyFile.Worksheets.Count - 1
       If MyFile.Worksheets(lngSheet).Name = SheetName.Value Then
           Set MySheet = MyFile.Worksheets(lngSheet)
           Exit For
       End If
   Next lngSheet

   If MySheet Is Nothing Then
       ' there is no such sheet in the file
       ' do what you need to do....
   Else
       ' there is such a sheet in the file
       (...)
   End If

   Set MySheet = Nothing
   MyFile.Application.ActiveWorkBook.Close False
   Set MyFile = Nothing
   xlApp.Quit
   Set xlApp = Nothing

   Set rstDebit = Nothing

Signature

       Ken Snell
<MS ACCESS MVP>

> How can I verify if one specific sheet is inside the spresdsheet?
> I need to verify if it exists, otherwise a error message of "Out of range"
[quoted text clipped - 16 lines]
>
> Thanks
Mauricio Silva - 26 Oct 2004 20:03 GMT
Hi Ken,

unfortunately, it did not work... at the first time the program pass thru the:
If MyFile.Worksheets(lngSheet).Name = SheetName.Value Then

it raises the error for Out of range... (weird for me !!!)

but looking into your code I was able to modify mine and it worked :

(...)
   Set xlApp = CreateObject("Excel.Application")
   Set MyFile = xlApp.WorkBooks.Open(FileName)
   
   On Error Resume Next
   Set MySheet = MyFile.Sheets(SheetName.Value)
   
   On Error GoTo ErrorHandler
   If MySheet Is Nothing Then
       Err.Raise 1, , "The specified sheet '" & SheetName.Value & "' is not
in the file"
   End If
(...)

Thanks Ken

> Try this:
>
[quoted text clipped - 26 lines]
>     xlApp.Quit
>     Set xlApp = Nothing
Ken Snell [MVP] - 27 Oct 2004 04:56 GMT
I think I had the wrong range on the For loop to find the worksheet. Try
this For loop in place of the one I'd posted:

For lngSheet = 1 To MyFile.Worksheets.Count
   If MyFile.Worksheets(lngSheet).Name = SheetName.Value Then
       Set MySheet = MyFile.Worksheets(lngSheet)
       Exit For
   End If
Next lngSheet

Good luck!

Signature

       Ken Snell
<MS ACCESS MVP>

> Hi Ken,
>
[quoted text clipped - 51 lines]
> >     xlApp.Quit
> >     Set xlApp = Nothing
Mauricio Silva - 28 Oct 2004 14:51 GMT
Man, it worked like a glove... thank you !!!

I have just one technical question:
I am opening an Excel file with 5 Sheets, why the command
MyFile.Worksheets.Count  is returning 10 ?

Thank you again

Mauricio Silva

> I think I had the wrong range on the For loop to find the worksheet. Try
> this For loop in place of the one I'd posted:
[quoted text clipped - 65 lines]
> > >     xlApp.Quit
> > >     Set xlApp = Nothing
Douglas J. Steele - 28 Oct 2004 15:46 GMT
Do you have charts or anything like that as well as the sheets? They're part
of the Worksheets collection as well.

Try the following to see what it thinks the 10 worksheets are:

For lngSheet = 1 To MyFile.Worksheets.Count
    Debug.Print MyFile.Worksheets(lngSheet).Name
Next lngSheet

Signature

Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

> Man, it worked like a glove... thank you !!!
>
[quoted text clipped - 75 lines]
> > > >     xlApp.Quit
> > > >     Set xlApp = Nothing
Mauricio Silva - 28 Oct 2004 17:29 GMT
What can I tell... I am getting this list
-March chargeable services
-no charge services
-Legend (*)
-Sheet2
-Studio Usage (*)
-Studio Usage Calc background (*)
-Summary (*)
-Price Assembly (*)
-Sheet3
-Sheet4

There is nothing hidden in this spreadsheet. The only extra object inside
the file is a picture and nothing else. I also could not reproduce this
creating a new file so, I won't bother you or myself with it...  :)

The funny thing is, I CAN select the *hidden* spreadsheet using the
routine...  80  whenever I have time, I will investigate. :D

Anyway... thanks for your help, you saved me of a lot of trouble !!!!

Take care
Mauricio Silva

> Do you have charts or anything like that as well as the sheets? They're part
> of the Worksheets collection as well.
[quoted text clipped - 89 lines]
> > > > >     xlApp.Quit
> > > > >     Set xlApp = Nothing
Ken Snell [MVP] - 28 Oct 2004 20:30 GMT
Which of the sheet names that you have posted are *not* seen by you in the
file?

Signature

       Ken Snell
<MS ACCESS MVP>

> What can I tell... I am getting this list
> -March chargeable services
[quoted text clipped - 113 lines]
> > > > > >     xlApp.Quit
> > > > > >     Set xlApp = Nothing
Mauricio Silva - 29 Oct 2004 19:21 GMT
Oops, sorry ... my fault...

I put the (*) to show which ones ARE showing up... the other ones are not
visible.
Is there some property like hidden or Visible whick I can verify?

For fun, I tryed to cread new sheets and the first came up with the name
Sheet1 (as expected) and the second one as Sheet5 (even not showing the other
ones)

Mauricio Silva

> Which of the sheet names that you have posted are *not* seen by you in the
> file?
[quoted text clipped - 32 lines]
> > >      Debug.Print MyFile.Worksheets(lngSheet).Name
> > >  Next lngSheet
Ken Snell [MVP] - 29 Oct 2004 22:12 GMT
If you use Format | Sheet | Unhide menu, you don't see the "missing" sheets
listed there? If they're hidden, they should show in that list.

If a sheet is hidden, then its Visible property is set to False.

Signature

       Ken Snell
<MS ACCESS MVP>

> Oops, sorry ... my fault...
>
[quoted text clipped - 44 lines]
> > > >      Debug.Print MyFile.Worksheets(lngSheet).Name
> > > >  Next lngSheet
Jamie Collins - 01 Nov 2004 13:30 GMT
> If you use Format | Sheet | Unhide menu, you don't see the "missing" sheets
> listed there? If they're hidden, they should show in that list.
>
> If a sheet is hidden, then its Visible property is set to False.

I think you meant to say, if a sheet is hidden, then its Visible
property will coerce to False. The Worksheet.Visible property is a
Long integer and relates to the Excel.XlSheetVisibility enumeration.

I'm not sure why you are enumerating all worksheets (which would
include hidden and very hidden sheets anyhow). Why not simply:

Set MySheet = Nothing
On Error Resume Next
Set MySheet = MyFile.Worksheets(SheetName.Value)
On Error Goto 0

If MySheet Is Nothing Then
 ' no such sheet ....
Else
 ' there is such a sheet ...
End If

Jamie.

--
Mauricio Silva - 01 Nov 2004 15:12 GMT
Now I understand... I was looking at Window | Unhide menu and this option is
disabled, then I thought some thing was wrong. I should be looking at  Format
| Sheet | Unhide menu... here I can see them all.

I know this is not a Excel list but thanks for the help in Excel.

Mauricio Silva

P.S. I am listing them all because my client wants to select the spreadsheet
to be imported.

Thanks again

> > If you use Format | Sheet | Unhide menu, you don't see the "missing" sheets
> > listed there? If they're hidden, they should show in that list.
[quoted text clipped - 22 lines]
>
> --
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.