Solved

Checking for existing Excel worksheets?

Posted on 2002-03-04
5
193 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 49

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now