Solved

Checking for existing Excel worksheets?

Posted on 2002-03-04
5
198 Views
Last Modified: 2010-05-02
How can I check that the worksheet SheetNo I want to read from really exists? I'm referencing it by xlBook.Worksheets(SheetNo) but I cant always be sure of the number of sheets in the Excel file.

Thanks.
0
Comment
Question by:mawe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6838687
Hi mawe,

Use the function below to test if a WorkSheet is exist

Private appExcel As Excel.Application
Private wbExcel As Excel.Workbook
Private WSheet As Excel.Worksheet

Set appExcel = CreateObject("Excel.Application")
Set wbExcel = appExcel.Workbooks.Open(txtPath.Text)

If DoesSheetExist("123") Then
'Exist
Else
'Does Not Exist
End If

and here is the Function:

Private Function DoesSheetExist(SheetName As String) As Boolean
    On Error Resume Next
    DoesSheetExist = wbExcel.Sheets(SheetName).Name = SheetName
End Function

Hope this help
0
 
LVL 4

Accepted Solution

by:
WolfgangKoenig earned 100 total points
ID: 6838749
Here sample code to check if a worksheet with the specific number !or! a specific name exists ...:
-----------------------------------------------------------

Private appExcel As Excel.Application
Private wbExcel As Excel.Workbook
Private WSheet As Excel.Worksheet

Private SheetNumber As Long
Private SheetName As String

Private Sub Command1_Click()
Set appExcel = CreateObject("Excel.Application")
Set wbExcel = appExcel.Workbooks.Open("C:\test.xls")

' To Test
SheetName = "Tabelle1"
SheetNumber = 1

' Test1: name ?
For Each WSheet In appExcel.Worksheets
If WSheet.Name = SheetName Then MsgBox ("Worksheet exists")
Next

' Test2: number ?
For Each WSheet In appExcel.Worksheets
If WSheet.Index = SheetNumber Then MsgBox ("Worksheet exists")
Next

End Sub


Best regards
WoK
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6839214
What a nice! same source maybe...:))
0
 

Author Comment

by:mawe
ID: 6841107
None of the suggestion was excactly what I needed but Wolfgangs' gave me some ideas so I'll give the points to him.

Thanks both.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6844099
mawe, what's wrong? According with your question, both comments could do the job?, why "B" grade?
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question