Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Checking for existing Excel worksheets?

Posted on 2002-03-04
5
Medium Priority
?
201 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 53

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 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