Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Checking if Sheet Exists?

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett

  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,316
Default Checking if Sheet Exists?

This function return TRUE if the worksheet is found, and FALSE if it isn't.
Just replace the value of the strMatch variable to the name of the worksheet
you're looking for:

Function WrkShtFound() As Boolean

Dim wb As Workbook
Dim ws As Worksheet
Dim strMatch As String
Dim strName As String
Dim blnIsFound As Boolean

On Error GoTo Err_Found

Set wb = ActiveWorkbook
strMatch = "Sheet Name You're Looking For"

For Each ws In wb.Worksheets
strName = ws.name
If strName = strMatch Then
blnIsFound = True
Exit For
Else
blnIsFound = False
End If
Next ws

Exit_Found:

Set wb = Nothing
Set ws = Nothing
WrkShtFound = blnIsFound
Exit Function

Err_Found:

If Err.Number 0 Then
MsgBox "An error has occurred while attempting to " & _
"verify that this workbook has a worksheet named " _
& strMatch & vbCrLf & vbCrLf & "Error Number: " & _
Err.Number & vbCrLf & vbCrLf & "Error Description: " & _
Err.Description, vbCritical + vbOKOnly, _
"Error Verifying Worksheet Names"
Err.Clear
blnIsFound = False
Resume Exit_Found
End If

End Function

--
Kevin Backmann


" wrote:

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 341
Default Checking if Sheet Exists?

Sub sheetfinder()

Dim wsYourSheet As Worksheet
Dim sSheetName As String

sSheetName = InputBox("sheet name?")
If sSheetName = "" Then Exit Sub

On Error Resume Next
Set wsYourSheet = Worksheets(sSheetName)
On Error GoTo 0

If wsYourSheet Is Nothing Then
Set wsYourSheet = Worksheets.Add
wsYourSheet.Name = sSheetName
Else
Worksheets(sSheetName).Activate
End If

'it is now there and activated and you can put code in here to format it

End Sub

please rate me
--
Allllen


" wrote:

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett


  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Checking if Sheet Exists?

Sub gsnu()
Dim w As Worksheet
For Each w In Worksheets
If w.Name = "trial" Then
MsgBox ("worksheet " & w.Name & " already exists")
Exit Sub
End If
Next
MsgBox ("worksheet " & "trial" & " does not exist")
End Sub

--
Gary''s Student


" wrote:

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Checking if Sheet Exists?

I like this function from Chip Pearson:

Function WorksheetExists(SheetName As Variant, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function

'and you can use it like:
....
if worksheetexists("myname",activeworkbook) then



wrote:

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 48
Default Checking if Sheet Exists?

This did it! Thank you!


Kevin B wrote:
This function return TRUE if the worksheet is found, and FALSE if it isn't.
Just replace the value of the strMatch variable to the name of the worksheet
you're looking for:

Function WrkShtFound() As Boolean

Dim wb As Workbook
Dim ws As Worksheet
Dim strMatch As String
Dim strName As String
Dim blnIsFound As Boolean

On Error GoTo Err_Found

Set wb = ActiveWorkbook
strMatch = "Sheet Name You're Looking For"

For Each ws In wb.Worksheets
strName = ws.name
If strName = strMatch Then
blnIsFound = True
Exit For
Else
blnIsFound = False
End If
Next ws

Exit_Found:

Set wb = Nothing
Set ws = Nothing
WrkShtFound = blnIsFound
Exit Function

Err_Found:

If Err.Number 0 Then
MsgBox "An error has occurred while attempting to " & _
"verify that this workbook has a worksheet named " _
& strMatch & vbCrLf & vbCrLf & "Error Number: " & _
Err.Number & vbCrLf & vbCrLf & "Error Description: " & _
Err.Description, vbCritical + vbOKOnly, _
"Error Verifying Worksheet Names"
Err.Clear
blnIsFound = False
Resume Exit_Found
End If

End Function

--
Kevin Backmann


" wrote:

Hi,

Is there a way in Visual basic to check if a worksheet with a specific
name exists? If it does not exist, I want to add it and format it; if
it does, I just want to format it.

Thanks!

Brett



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro unouwanme Excel Discussion (Misc queries) 9 August 31st 06 09:38 PM
Asked previously...can this not be done in excel simonsmith Excel Discussion (Misc queries) 2 May 16th 06 11:50 PM
Compiling macro based on cell values simonsmith Excel Discussion (Misc queries) 1 May 16th 06 08:31 PM
Using a relative SHEET reference for source data in a chart James Charts and Charting in Excel 6 August 16th 05 05:07 PM
Checking if sheet is visible LAF Excel Discussion (Misc queries) 1 August 9th 05 09:20 PM


All times are GMT +1. The time now is 07:33 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"