?
Solved

Checking for existing Excel worksheets?

Posted on 2002-03-04
5
Medium Priority
?
202 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
5 Comments
 
LVL 54

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 300 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

809 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