Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 1,388
Default Create Worksheet BUT If It Already Exists...

I have the following:

Sub EXAMPLE()
Dim 1WorkSheet As String
1WorkSheet = "Sheet One"
Sheets.Add.Name = 1WorkSheet
Worksheets(1WorkSheet).Visible = xlSheetHidden
End Sub

How do I check, before 1WorkSheet is created, that there is not already a
sheet called "Sheet One". And if there is, use the existing sheet, first of
all erasing data in it and carrying on as normal?

Thanks!
Dave
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 229
Default Create Worksheet BUT If It Already Exists...

Here's how I would go about it.


Public Sub EXAMPLE()
Dim wsh As Excel.Worksheet

Dim strName As String
strName = "Sheet One"

On Error Resume Next
Set wsh = ThisWorkbook.Worksheets(strName)
On Error GoTo 0

If wsh Is Nothing Then
' does not exist
Set wsh = ThisWorkbook.Worksheets.Add
wsh.Name = strName
Else
wsh.UsedRange.Clear
End If

wsh.Visible = xlSheetHidden
End Sub


On Oct 30, 5:55 pm, Dave wrote:
I have the following:

Sub EXAMPLE()
Dim 1WorkSheet As String
1WorkSheet = "Sheet One"
Sheets.Add.Name = 1WorkSheet
Worksheets(1WorkSheet).Visible = xlSheetHidden
End Sub

How do I check, before 1WorkSheet is created, that there is not already a
sheet called "Sheet One". And if there is, use the existing sheet, first of
all erasing data in it and carrying on as normal?

Thanks!
Dave



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,124
Default Create Worksheet BUT If It Already Exists...

Adapt this to suit
Sub ifsheet()
Application.DisplayAlerts = False
Dim mySheet As String
mySheet = "sheet7"
If mySheet = "" Then Exit Sub
On Error Resume Next
If Sheets(mySheet) Is Nothing Then
MsgBox "no"
'sheets.add
Else
MsgBox "Yes"
'do your thing
'sheets(mysheet).cells.clear'or?

End If
Application.DisplayAlerts = True
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"Dave" wrote in message
...
I have the following:

Sub EXAMPLE()
Dim 1WorkSheet As String
1WorkSheet = "Sheet One"
Sheets.Add.Name = 1WorkSheet
Worksheets(1WorkSheet).Visible = xlSheetHidden
End Sub

How do I check, before 1WorkSheet is created, that there is not already a
sheet called "Sheet One". And if there is, use the existing sheet, first
of
all erasing data in it and carrying on as normal?

Thanks!
Dave


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
Show it here, if it exists there Kevin Excel Discussion (Misc queries) 5 July 28th 07 06:21 PM
Can I create a worksheet menu to select each other worksheet pippagrace Excel Discussion (Misc queries) 4 June 23rd 06 01:28 PM
File Exists Mike McLellan Excel Discussion (Misc queries) 2 May 4th 06 09:20 AM
check if worksheet exists joeeng Excel Worksheet Functions 3 September 7th 05 06:49 PM
Need help with a function I'm not sure exists Cervantes Excel Worksheet Functions 7 August 16th 05 06:38 AM


All times are GMT +1. The time now is 12:00 PM.

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"