PC Review


Reply
Thread Tools Rate Thread

create new sheet with zip code as name

 
 
Rich
Guest
Posts: n/a
 
      27th Jul 2009
Am importing some sales records that I am trying to keep track of by zip
codes. Want to create a new sheet if one doesn't already exist from the
records (G1 thru ???) and use the zip for the name of the sheet. Am a novice,
so please be specific with suggested code.
 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      27th Jul 2009
Try this code. Change sheet name to sheet where zipcodes are located.

Sub createsheets()

'set sheet with zipcodes
Set MasterSht = Sheets("Sheet1")

With MasterSht
RowCount = 1
'loop until blank cell is found
Do While .Range("G" & RowCount) <> ""
zipcode = .Range("G" & RowCount)
'check each sheet for zipcode name
Found = False
For Each sht In Sheets
If sht.Name = zipcode Then
Found = True
Exit For
End If

Next sht

'if zipcode not found
If Found = False Then
Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
newsht.Name = zipcode

End If

RowCount = RowCount + 1
Loop
End With
End Sub


"Rich" wrote:

> Am importing some sales records that I am trying to keep track of by zip
> codes. Want to create a new sheet if one doesn't already exist from the
> records (G1 thru ???) and use the zip for the name of the sheet. Am a novice,
> so please be specific with suggested code.

 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      27th Jul 2009
Thanks. But I told you wrong. Col N not G. Works fine on all columns BUT N.
Have tried changing format cells for that column, but nothing seems to work.
Get same error on compile. Help!

"Joel" wrote:

> Try this code. Change sheet name to sheet where zipcodes are located.
>
> Sub createsheets()
>
> 'set sheet with zipcodes
> Set MasterSht = Sheets("Sheet1")
>
> With MasterSht
> RowCount = 1
> 'loop until blank cell is found
> Do While .Range("G" & RowCount) <> ""
> zipcode = .Range("G" & RowCount)
> 'check each sheet for zipcode name
> Found = False
> For Each sht In Sheets
> If sht.Name = zipcode Then
> Found = True
> Exit For
> End If
>
> Next sht
>
> 'if zipcode not found
> If Found = False Then
> Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
> newsht.Name = zipcode
>
> End If
>
> RowCount = RowCount + 1
> Loop
> End With
> End Sub
>
>
> "Rich" wrote:
>
> > Am importing some sales records that I am trying to keep track of by zip
> > codes. Want to create a new sheet if one doesn't already exist from the
> > records (G1 thru ???) and use the zip for the name of the sheet. Am a novice,
> > so please be specific with suggested code.

 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      28th Jul 2009
Seems to crash second time through on any column. Error 1004 which I think is
trying to write a new record when one already exists (sheet name). When I
delete all sheets except master and do it again, works fine. But if I repeat
without deleting sheets, crashes. I can't firgure out what is wrong with the
code, unless it has to do with the resetting of "sht".

"Rich" wrote:

> Thanks. But I told you wrong. Col N not G. Works fine on all columns BUT N.
> Have tried changing format cells for that column, but nothing seems to work.
> Get same error on compile. Help!
>
> "Joel" wrote:
>
> > Try this code. Change sheet name to sheet where zipcodes are located.
> >
> > Sub createsheets()
> >
> > 'set sheet with zipcodes
> > Set MasterSht = Sheets("Sheet1")
> >
> > With MasterSht
> > RowCount = 1
> > 'loop until blank cell is found
> > Do While .Range("G" & RowCount) <> ""
> > zipcode = .Range("G" & RowCount)
> > 'check each sheet for zipcode name
> > Found = False
> > For Each sht In Sheets
> > If sht.Name = zipcode Then
> > Found = True
> > Exit For
> > End If
> >
> > Next sht
> >
> > 'if zipcode not found
> > If Found = False Then
> > Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
> > newsht.Name = zipcode
> >
> > End If
> >
> > RowCount = RowCount + 1
> > Loop
> > End With
> > End Sub
> >
> >
> > "Rich" wrote:
> >
> > > Am importing some sales records that I am trying to keep track of by zip
> > > codes. Want to create a new sheet if one doesn't already exist from the
> > > records (G1 thru ???) and use the zip for the name of the sheet. Am a novice,
> > > so please be specific with suggested code.

 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      28th Jul 2009
Rich,

To fix your problems, use this macro, written assuming you have headers in
row 1, and your sheet is name Master Sheet

Sub MakeZipCodeSheets()

Dim mySht As Worksheet
Dim myArea As Range
Dim myCell As Range
Dim myName As String

Set mySht = Worksheets("Master Sheet")
Set myArea = Intersect(mySht.UsedRange, _
mySht.Range("N2:N" & Rows.Count))

On Error GoTo NoSheet

For Each myCell In myArea
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(before:=Worksheets(1))
mySht.Name = myCell.Value
Resume
SheetExists:
Next myCell

End Sub


HTH,
Bernie
MS Excel MVP

"Rich" <(E-Mail Removed)> wrote in message
news:5BE3D0F2-B1A9-4236-A283-(E-Mail Removed)...
> Seems to crash second time through on any column. Error 1004 which I think
> is
> trying to write a new record when one already exists (sheet name). When I
> delete all sheets except master and do it again, works fine. But if I
> repeat
> without deleting sheets, crashes. I can't firgure out what is wrong with
> the
> code, unless it has to do with the resetting of "sht".
>
> "Rich" wrote:
>
>> Thanks. But I told you wrong. Col N not G. Works fine on all columns BUT
>> N.
>> Have tried changing format cells for that column, but nothing seems to
>> work.
>> Get same error on compile. Help!
>>
>> "Joel" wrote:
>>
>> > Try this code. Change sheet name to sheet where zipcodes are located.
>> >
>> > Sub createsheets()
>> >
>> > 'set sheet with zipcodes
>> > Set MasterSht = Sheets("Sheet1")
>> >
>> > With MasterSht
>> > RowCount = 1
>> > 'loop until blank cell is found
>> > Do While .Range("G" & RowCount) <> ""
>> > zipcode = .Range("G" & RowCount)
>> > 'check each sheet for zipcode name
>> > Found = False
>> > For Each sht In Sheets
>> > If sht.Name = zipcode Then
>> > Found = True
>> > Exit For
>> > End If
>> >
>> > Next sht
>> >
>> > 'if zipcode not found
>> > If Found = False Then
>> > Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
>> > newsht.Name = zipcode
>> >
>> > End If
>> >
>> > RowCount = RowCount + 1
>> > Loop
>> > End With
>> > End Sub
>> >
>> >
>> > "Rich" wrote:
>> >
>> > > Am importing some sales records that I am trying to keep track of by
>> > > zip
>> > > codes. Want to create a new sheet if one doesn't already exist from
>> > > the
>> > > records (G1 thru ???) and use the zip for the name of the sheet. Am a
>> > > novice,
>> > > so please be specific with suggested code.


 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      28th Jul 2009
My code just need a declararation statement

Dim zipcode As String

Because zicode are numbers and sheet names are strings
zipcode = sht.name where
10001 <> "10001"

adding the declaration make zipcode and string and then the sheet name will
equal the value in the worksheet.



"Rich" wrote:

> Seems to crash second time through on any column. Error 1004 which I think is
> trying to write a new record when one already exists (sheet name). When I
> delete all sheets except master and do it again, works fine. But if I repeat
> without deleting sheets, crashes. I can't firgure out what is wrong with the
> code, unless it has to do with the resetting of "sht".
>
> "Rich" wrote:
>
> > Thanks. But I told you wrong. Col N not G. Works fine on all columns BUT N.
> > Have tried changing format cells for that column, but nothing seems to work.
> > Get same error on compile. Help!
> >
> > "Joel" wrote:
> >
> > > Try this code. Change sheet name to sheet where zipcodes are located.
> > >
> > > Sub createsheets()
> > >
> > > 'set sheet with zipcodes
> > > Set MasterSht = Sheets("Sheet1")
> > >
> > > With MasterSht
> > > RowCount = 1
> > > 'loop until blank cell is found
> > > Do While .Range("G" & RowCount) <> ""
> > > zipcode = .Range("G" & RowCount)
> > > 'check each sheet for zipcode name
> > > Found = False
> > > For Each sht In Sheets
> > > If sht.Name = zipcode Then
> > > Found = True
> > > Exit For
> > > End If
> > >
> > > Next sht
> > >
> > > 'if zipcode not found
> > > If Found = False Then
> > > Set newsht = Sheets.Add(after:=Sheets(Sheets.Count))
> > > newsht.Name = zipcode
> > >
> > > End If
> > >
> > > RowCount = RowCount + 1
> > > Loop
> > > End With
> > > End Sub
> > >
> > >
> > > "Rich" wrote:
> > >
> > > > Am importing some sales records that I am trying to keep track of by zip
> > > > codes. Want to create a new sheet if one doesn't already exist from the
> > > > records (G1 thru ???) and use the zip for the name of the sheet. Am a novice,
> > > > so please be specific with suggested code.

 
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
How to create a function in a code sheet Mathieu936 Microsoft Excel Programming 6 14th Jun 2010 03:31 PM
Use VBA to create new sheet with event handlers in sheet's code VBAer Microsoft Excel Programming 2 24th Nov 2009 07:22 AM
Create Bar code Sheet w/lookups, index/match KalliKay Microsoft Excel Worksheet Functions 3 29th Sep 2008 08:05 PM
Code to create atbs in excel sheet deepa prabhakar Microsoft Excel Misc 1 26th May 2006 09:28 PM
Code to create and name a sheet =?Utf-8?B?ZG9ncGlnZmlzaA==?= Microsoft Excel Programming 8 5th Jan 2006 03:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:10 AM.