PC Review


Reply
Thread Tools Rate Thread

Create & Name new Sheets without duplicates

 
 
hughaskew2@yahoo.com
Guest
Posts: n/a
 
      29th Dec 2006
Need to create new worksheets from a template ws, then name them from a
list on a sheet called "CatNames".
The sub below from Dave P. works fine, except i would like to avoid
creating duplicate sheets.
If my list in Col A contains 12 names, i insert the new worksheets.
that works fine. if i add 8 names to the list tomorrow, i need to
create ONLY the 8 new sheets. make sense?

any help offered will be gladly received.
Many TIA

Sub CreateNameSheets()
' by Dave Peterson

Dim TemplateWks As Worksheet
Dim ListWks As Worksheet
Dim ListRng As Range
Dim myCell As Range

Set TemplateWks = Worksheets("qwerty")
Set ListWks = Worksheets("CatNames")
With ListWks
Set ListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
End With

For Each myCell In ListRng.Cells
TemplateWks.Copy after:=Worksheets(Worksheets.Count)
On Error Resume Next
ActiveSheet.Name = myCell.Value
If Err.Number <> 0 Then
MsgBox "Duplicate Worksheet " & ActiveSheet.Name
Err.Clear
End If
On Error GoTo 0
Next myCell

End Sub

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      29th Dec 2006
Check to see if a worksheet with that name exists before adding it:

Option Explicit
Sub CreateNameSheets()
' by Dave Peterson

Dim TemplateWks As Worksheet
Dim ListWks As Worksheet
Dim ListRng As Range
Dim myCell As Range
dim Wks as worksheet
dim resp as long

Set TemplateWks = Worksheets("qwerty")
Set ListWks = Worksheets("CatNames")
With ListWks
Set ListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
End With

For Each myCell In ListRng.Cells
set wks = nothing
on error resume next
set wks = worksheets(mycell.value)
on error goto 0
if wks is nothing then
'doesn't exist, so add it
TemplateWks.Copy _
after:=Worksheets(Worksheets.Count)
on error resume next
ActiveSheet.Name = myCell.Value
if err.number <> 0 then
'delete it?
application.displayalerts = false
activesheet.delete
application.displayalerts = true
err.clear
end if
else
beep
'or
msgbox mycell.value & " already exists"
end if
Next myCell

End Sub

I deleted the newly added sheet if the name was invalid.



(E-Mail Removed) wrote:
>
> Need to create new worksheets from a template ws, then name them from a
> list on a sheet called "CatNames".
> The sub below from Dave P. works fine, except i would like to avoid
> creating duplicate sheets.
> If my list in Col A contains 12 names, i insert the new worksheets.
> that works fine. if i add 8 names to the list tomorrow, i need to
> create ONLY the 8 new sheets. make sense?
>
> any help offered will be gladly received.
> Many TIA
>
> Sub CreateNameSheets()
> ' by Dave Peterson
>
> Dim TemplateWks As Worksheet
> Dim ListWks As Worksheet
> Dim ListRng As Range
> Dim myCell As Range
>
> Set TemplateWks = Worksheets("qwerty")
> Set ListWks = Worksheets("CatNames")
> With ListWks
> Set ListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
> End With
>
> For Each myCell In ListRng.Cells
> TemplateWks.Copy after:=Worksheets(Worksheets.Count)
> On Error Resume Next
> ActiveSheet.Name = myCell.Value
> If Err.Number <> 0 Then
> MsgBox "Duplicate Worksheet " & ActiveSheet.Name
> Err.Clear
> End If
> On Error GoTo 0
> Next myCell
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
hughaskew2@yahoo.com
Guest
Posts: n/a
 
      29th Dec 2006
Dave - MANY Thanks! works like unto a treat!!!
Happy NW





Dave Peterson wrote:
> Check to see if a worksheet with that name exists before adding it:
>
> Option Explicit
> Sub CreateNameSheets()
> ' by Dave Peterson
>
> Dim TemplateWks As Worksheet
> Dim ListWks As Worksheet
> Dim ListRng As Range
> Dim myCell As Range
> dim Wks as worksheet
> dim resp as long
>
> Set TemplateWks = Worksheets("qwerty")
> Set ListWks = Worksheets("CatNames")
> With ListWks
> Set ListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
> End With
>
> For Each myCell In ListRng.Cells
> set wks = nothing
> on error resume next
> set wks = worksheets(mycell.value)
> on error goto 0
> if wks is nothing then
> 'doesn't exist, so add it
> TemplateWks.Copy _
> after:=Worksheets(Worksheets.Count)
> on error resume next
> ActiveSheet.Name = myCell.Value
> if err.number <> 0 then
> 'delete it?
> application.displayalerts = false
> activesheet.delete
> application.displayalerts = true
> err.clear
> end if
> else
> beep
> 'or
> msgbox mycell.value & " already exists"
> end if
> Next myCell
>
> End Sub
>
> I deleted the newly added sheet if the name was invalid.
>
>
>
> (E-Mail Removed) wrote:
> >
> > Need to create new worksheets from a template ws, then name them from a
> > list on a sheet called "CatNames".
> > The sub below from Dave P. works fine, except i would like to avoid
> > creating duplicate sheets.
> > If my list in Col A contains 12 names, i insert the new worksheets.
> > that works fine. if i add 8 names to the list tomorrow, i need to
> > create ONLY the 8 new sheets. make sense?
> >
> > any help offered will be gladly received.
> > Many TIA
> >
> > Sub CreateNameSheets()
> > ' by Dave Peterson
> >
> > Dim TemplateWks As Worksheet
> > Dim ListWks As Worksheet
> > Dim ListRng As Range
> > Dim myCell As Range
> >
> > Set TemplateWks = Worksheets("qwerty")
> > Set ListWks = Worksheets("CatNames")
> > With ListWks
> > Set ListRng = .Range("a1", .Cells(.Rows.Count, "A").End(xlUp))
> > End With
> >
> > For Each myCell In ListRng.Cells
> > TemplateWks.Copy after:=Worksheets(Worksheets.Count)
> > On Error Resume Next
> > ActiveSheet.Name = myCell.Value
> > If Err.Number <> 0 Then
> > MsgBox "Duplicate Worksheet " & ActiveSheet.Name
> > Err.Clear
> > End If
> > On Error GoTo 0
> > Next myCell
> >
> > End Sub

>
> --
>
> Dave Peterson


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting duplicates between sheets rabidsanity@gmail.com Microsoft Excel Programming 1 4th Oct 2008 04:48 PM
Multiple Sheets (Need to create 500 individual sheets in one workbook, pulling DATA Amaxwell Microsoft Excel Worksheet Functions 4 17th Aug 2006 06:23 AM
counting duplicates Among Many Sheets, Possible?? Mhz Microsoft Excel New Users 5 5th Jul 2006 02:23 AM
duplicates across sheets davewars Microsoft Excel Misc 11 24th May 2004 09:02 AM
Deleting duplicates across sheets jimmy Microsoft Excel Programming 4 26th Sep 2003 08:08 PM


Features
 

Advertising
 

Newsgroups
 


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