View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Varun Varun is offline
external usenet poster
 
Posts: 37
Default How to check to see if a sheet with a particular name exists?

Hi,

VBA newbie here...I have written below code which calls out a sub and func.
Problem is that when the sub procedure is called, which in turn invokes the
function, I am unable to get the correct output. I want to check to see if
the opened file has a worksheet named Impedance and if not, produce a message
saying so...but problem is that I always get "This is not TTM Layer stackup
file" message. What am I doing wrong?

Thanks for help.


Here's the code snippet.

Private Sub cmdbut_Click()

'Make sure that either TTM or DDI is selected
If ttm.Value = False And ddi.Value = False Then
MsgBox "You must select the file type before proceeding", , "File
Not Selected"
Exit Sub
Else
If ttm.Value = True Then
'opening ttm file
ttmfn = Application.GetOpenFilename(FileFilter:="Excel Files
(*.xls), *.xls", _
Title:="Please select a file")
If ttmfn = False Then
MsgBox "Stopping because you did not select a file"
Exit Sub
Else
MsgBox ttmfn, , "File Name"
Application.Visible = False
Workbooks.Open (ttmfn)
End If


Workbooks(1).Activate
'Call DoesSheetExist
'Worksheets("Impedance").Activate

Call WorksheetCheck(ttmfn)

End If
End If

End Sub


'SUB:

Sub WorksheetCheck(ttmfn)
Workbooks(1).Activate
If SheetExists("Impedance") = True Then
MsgBox "Click OK to Continue", vbOKCancel, "Continue"
Else
MsgBox "This is not TTM layerstackup file", , "Wrong File"
End If
End Sub



'FUNCTION:

Function SheetExists(Impedance As String) As Boolean
Dim sheetcount As Integer
Dim t As Integer

SheetExists = False
sheetcount = ActiveWorkbook.Sheets.Count
For t = 1 To sheetcount
If Sheets(t).Name = "Impedance" Then
SheetExists = True
Exit Function
End If
Next t
End Function