Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default Worksheets Exists Code...

Hi,
I'm using the code below to find out if a worksheet named "Memo" exists in
the active workbook.
If the code find a worksheet named "Memo"... it returns True.
It's working great..but now I need to change the code to find out if there
are any worksheets in the active workbook that start with the name "Memo"
There can be many worksheets with the name "Memo" added to this file....and
appear as... Memo, Memo(2), Memo (3) and so on...
I'm not sure what needs to be changed in this code to do this..

Public Function WSExist(Memo As String) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Object
On Error Resume Next
WSExist = False
Set objWorksheet = ActiveWorkbook.Sheets("Memo")
If Err = 0 Then
WSExist = True
End If
End Function

Any help is greatly apprecaited..

Thanks in advance!
Kimberly :)


  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default Worksheets Exists Code...

Hi Kimberly,

Try this:

Public Function WSExist(MyStr As String) As Boolean
'returns true if worksheet exists in the active workbook
For Each ws In ActiveWorkbook.Worksheets
If UCase(ws.Name) Like UCase(MyStr) & "*" Then
WSExist = True
Exit For
End If
Next ws
End Function


Regards,
KL



"KimberlyC" wrote in message
...
Hi,
I'm using the code below to find out if a worksheet named "Memo" exists in
the active workbook.
If the code find a worksheet named "Memo"... it returns True.
It's working great..but now I need to change the code to find out if there
are any worksheets in the active workbook that start with the name "Memo"
There can be many worksheets with the name "Memo" added to this
file....and
appear as... Memo, Memo(2), Memo (3) and so on...
I'm not sure what needs to be changed in this code to do this..

Public Function WSExist(Memo As String) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Object
On Error Resume Next
WSExist = False
Set objWorksheet = ActiveWorkbook.Sheets("Memo")
If Err = 0 Then
WSExist = True
End If
End Function

Any help is greatly apprecaited..

Thanks in advance!
Kimberly :)




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Worksheets Exists Code...

One way:

Option Explicit

Public Function WSExist2(Memo As String, _
Optional wkbk As Workbook = Nothing) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Worksheet

If wkbk Is Nothing Then
Set wkbk = ActiveWorkbook
End If

WSExist2 = False
For Each objWorksheet In wkbk.Worksheets
If LCase(Left(objWorksheet.Name, Len(Memo))) = LCase(Memo) Then
WSExist2 = True
Exit For
End If
Next objWorksheet

End Function


Sub testme()
Dim wkbk As Workbook
Set wkbk = Workbooks("book2.xls")
MsgBox WSExist2("Memo", wkbk)
End Sub





KimberlyC wrote:

Hi,
I'm using the code below to find out if a worksheet named "Memo" exists in
the active workbook.
If the code find a worksheet named "Memo"... it returns True.
It's working great..but now I need to change the code to find out if there
are any worksheets in the active workbook that start with the name "Memo"
There can be many worksheets with the name "Memo" added to this file....and
appear as... Memo, Memo(2), Memo (3) and so on...
I'm not sure what needs to be changed in this code to do this..

Public Function WSExist(Memo As String) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Object
On Error Resume Next
WSExist = False
Set objWorksheet = ActiveWorkbook.Sheets("Memo")
If Err = 0 Then
WSExist = True
End If
End Function

Any help is greatly apprecaited..

Thanks in advance!
Kimberly :)


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 124
Default Worksheets Exists Code...

Thanks to you both!!!


"KimberlyC" wrote in message
...
Hi,
I'm using the code below to find out if a worksheet named "Memo" exists in
the active workbook.
If the code find a worksheet named "Memo"... it returns True.
It's working great..but now I need to change the code to find out if there
are any worksheets in the active workbook that start with the name "Memo"
There can be many worksheets with the name "Memo" added to this

file....and
appear as... Memo, Memo(2), Memo (3) and so on...
I'm not sure what needs to be changed in this code to do this..

Public Function WSExist(Memo As String) As Boolean
'returns true if worksheet exists in the active workbook
Dim objWorksheet As Object
On Error Resume Next
WSExist = False
Set objWorksheet = ActiveWorkbook.Sheets("Memo")
If Err = 0 Then
WSExist = True
End If
End Function

Any help is greatly apprecaited..

Thanks in advance!
Kimberly :)




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
Worksheet Exists? Invisible Worksheets? Ken Loomis Excel Programming 1 July 1st 05 01:25 AM
Help with code that checks if a sheet exists wachen Excel Programming 2 February 10th 04 02:39 AM
Printing multiple worksheets only if data exists hailnorm Excel Programming 6 January 10th 04 07:09 PM
Need code to protect worksheets - amount of worksheets varies Sandy[_3_] Excel Programming 1 September 9th 03 02:17 AM
If Worksheets("Test") Exists Then Delete It Don[_7_] Excel Programming 0 July 22nd 03 12:09 PM


All times are GMT +1. The time now is 03:44 PM.

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

About Us

"It's about Microsoft Excel"