Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default how do i chk is sheet exists my attempt included

hi
I have a list of sheet name in a1:a10
and what I want to do is go through each cell in that range and chk if a
work sheet exists with that name
if it does not, create a sheet in that name and them move on to the next
cell in the list
this is what i have so far

can some one point me in the right direction

Dim scv As Range
Set scv = ActiveSheet.Range("a1:a10")
Dim rng As Range
For Each rng In scv
If rng.Value 0 Then
Dim wks_Sht As Worksheet

For Each wks_Sht In Workbooks(ActiveWorkbook.Name).Worksheets
If wks_Sht.Name = rng.Value Then

Exit For
Else
If wks_Sht.Name < rng.Value Then
Sheets.Add.Name = rng.Value
Exit For
End If
End If


Next wks_Sht
End If
Next rng

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default how do i chk is sheet exists my attempt included

What you have is mighty close... Give this a try...

Sub AddSheets()
Dim wks As Worksheet
Dim rngCurrent As Range
Dim rngToSearch As Range

Set wks = Sheets("Start")
Set rngToSearch = wks.Range("A1:A10")

On Error GoTo AddSheet
For Each rngCurrent In rngToSearch
If rngCurrent.Value < Empty Then _
Set wks = Sheets(rngCurrent.Value)
Next rngCurrent
On Error GoTo 0

Exit Sub
AddSheet:
Set wks = Sheets.Add
wks.Name = rngCurrent.Value
Resume Next
End Sub
--
HTH...

Jim Thomlinson


"andy" wrote:

hi
I have a list of sheet name in a1:a10
and what I want to do is go through each cell in that range and chk if a
work sheet exists with that name
if it does not, create a sheet in that name and them move on to the next
cell in the list
this is what i have so far

can some one point me in the right direction

Dim scv As Range
Set scv = ActiveSheet.Range("a1:a10")
Dim rng As Range
For Each rng In scv
If rng.Value 0 Then
Dim wks_Sht As Worksheet

For Each wks_Sht In Workbooks(ActiveWorkbook.Name).Worksheets
If wks_Sht.Name = rng.Value Then

Exit For
Else
If wks_Sht.Name < rng.Value Then
Sheets.Add.Name = rng.Value
Exit For
End If
End If


Next wks_Sht
End If
Next rng

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default how do i chk is sheet exists my attempt included

brilliant thankyou



"Jim Thomlinson" wrote in message
...
What you have is mighty close... Give this a try...

Sub AddSheets()
Dim wks As Worksheet
Dim rngCurrent As Range
Dim rngToSearch As Range

Set wks = Sheets("Start")
Set rngToSearch = wks.Range("A1:A10")

On Error GoTo AddSheet
For Each rngCurrent In rngToSearch
If rngCurrent.Value < Empty Then _
Set wks = Sheets(rngCurrent.Value)
Next rngCurrent
On Error GoTo 0

Exit Sub
AddSheet:
Set wks = Sheets.Add
wks.Name = rngCurrent.Value
Resume Next
End Sub
--
HTH...

Jim Thomlinson


"andy" wrote:

hi
I have a list of sheet name in a1:a10
and what I want to do is go through each cell in that range and chk if a
work sheet exists with that name
if it does not, create a sheet in that name and them move on to the next
cell in the list
this is what i have so far

can some one point me in the right direction

Dim scv As Range
Set scv = ActiveSheet.Range("a1:a10")
Dim rng As Range
For Each rng In scv
If rng.Value 0 Then
Dim wks_Sht As Worksheet

For Each wks_Sht In Workbooks(ActiveWorkbook.Name).Worksheets
If wks_Sht.Name = rng.Value Then

Exit For
Else
If wks_Sht.Name < rng.Value Then
Sheets.Add.Name = rng.Value
Exit For
End If
End If


Next wks_Sht
End If
Next rng

End Sub





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default how do i chk is sheet exists my attempt included


Give this modification to your code a try:


Code:
--------------------
Sub ChgSheets()
Dim scv As Range
Set scv = ActiveSheet.Range("a1:a10")
Dim rng As Range
Dim wksFound As Boolean
For Each rng In scv
wksFound = False
If rng.Value 0 Then
Dim wks_Sht As Worksheet

For Each wks_Sht In Workbooks(ActiveWorkbook.Name).Worksheets
If wks_Sht.Name = rng.Value Then
wksFound = True
Exit For
End If
Next wks_Sht
If wksFound = False Then
Sheets.Add.Name = rng.Value
End If
End If
Next rng

End Sub
--------------------


You could also use error handling to check for the existence of each
sheet name in your range and then create the sheet if it doesn't
exist.

HTH


--
bhofsetz
------------------------------------------------------------------------
bhofsetz's Profile: http://www.excelforum.com/member.php...o&userid=18807
View this thread: http://www.excelforum.com/showthread...hreadid=380692

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
Protected sheet - attempt to delete row = unmerged cells Kaz Excel Discussion (Misc queries) 5 October 29th 09 05:28 PM
How to check to see if a sheet with a particular name exists? Varun Excel Worksheet Functions 3 January 25th 09 01:41 PM
Sheet name already exists eddie_zoom Excel Discussion (Misc queries) 1 March 11th 05 02:53 PM
How can I know if a sheet exists ? Ben.C Excel Programming 3 December 29th 03 09:36 AM
Testing to see if a sheet name exists anita Excel Programming 1 September 4th 03 10:14 PM


All times are GMT +1. The time now is 04:05 AM.

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"