Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Group and Create New Sheets

I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Group and Create New Sheets

K.I.S.S.,

Try the macro below, first selecting all your data and answering 2 when prompted.

HTH,
Bernie
MS Excel MVP


Sub ExportDatabaseToSeparateSheets()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer

myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")


Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells

Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)

For Each myCell In myArea
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
'These lines copy everything - including extra header rows
' and any SUBTOTAL formulas separated by blank row
'Uncomment them to use them
' myCell.Parent.Cells.SpecialCells(xlCellTypeVisible ).Copy
' mySht.Range("A1").PasteSpecial xlPasteValues


'These are the default - only copy the database values
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell

End Sub

"Keep It Simple Stupid" wrote in message
...
I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Group and Create New Sheets

An error message tells me that "Cannot rename a sheet to the same name as
another sheet, a referenced object library or a workbook reference by Visual
Basic"
Is it trying to create a new sheet for every row entry or did I forget to
change something?


"Bernie Deitrick" wrote:

K.I.S.S.,

Try the macro below, first selecting all your data and answering 2 when prompted.

HTH,
Bernie
MS Excel MVP


Sub ExportDatabaseToSeparateSheets()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer

myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")


Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells

Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)

For Each myCell In myArea
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
'These lines copy everything - including extra header rows
' and any SUBTOTAL formulas separated by blank row
'Uncomment them to use them
' myCell.Parent.Cells.SpecialCells(xlCellTypeVisible ).Copy
' mySht.Range("A1").PasteSpecial xlPasteValues


'These are the default - only copy the database values
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell

End Sub

"Keep It Simple Stupid" wrote in message
...
I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Group and Create New Sheets

It might be the numbering.

Try changing:

myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value

to

myName = Worksheets(Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName

If that doesn't work, would adding a prefix to the number cause you problems?

If not, change the code to

myName = Worksheets("Sht " & Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName


HTH,
Bernie
MS Excel MVP


"Keep It Simple Stupid" wrote in message
...
An error message tells me that "Cannot rename a sheet to the same name as
another sheet, a referenced object library or a workbook reference by Visual
Basic"
Is it trying to create a new sheet for every row entry or did I forget to
change something?


"Bernie Deitrick" wrote:

K.I.S.S.,

Try the macro below, first selecting all your data and answering 2 when prompted.

HTH,
Bernie
MS Excel MVP


Sub ExportDatabaseToSeparateSheets()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer

myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")


Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells

Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)

For Each myCell In myArea
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
'These lines copy everything - including extra header rows
' and any SUBTOTAL formulas separated by blank row
'Uncomment them to use them
' myCell.Parent.Cells.SpecialCells(xlCellTypeVisible ).Copy
' mySht.Range("A1").PasteSpecial xlPasteValues


'These are the default - only copy the database values
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell

End Sub

"Keep It Simple Stupid" wrote in message
...
I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 45
Default Group and Create New Sheets

I still can't get it to rename the new sheet as the value in Column B. I
don't know how to fix this. I do really need the "group value" as the sheet
name (whether or not it has a prefix). It always goofs when it gets to:

Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName

(And I have tried replacing myName with myCell.Value. It seems to work
when I put a specific name to the sheet like "Sheet 1", but as you can
imagine, it will not work when it gets around to creating the next sheet
because there is already a sheet named "Sheet 1")

Any ideas?

"Bernie Deitrick" wrote:

It might be the numbering.

Try changing:

myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value

to

myName = Worksheets(Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName

If that doesn't work, would adding a prefix to the number cause you problems?

If not, change the code to

myName = Worksheets("Sht " & Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName


HTH,
Bernie
MS Excel MVP


"Keep It Simple Stupid" wrote in message
...
An error message tells me that "Cannot rename a sheet to the same name as
another sheet, a referenced object library or a workbook reference by Visual
Basic"
Is it trying to create a new sheet for every row entry or did I forget to
change something?


"Bernie Deitrick" wrote:

K.I.S.S.,

Try the macro below, first selecting all your data and answering 2 when prompted.

HTH,
Bernie
MS Excel MVP


Sub ExportDatabaseToSeparateSheets()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer

myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")


Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells

Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)

For Each myCell In myArea
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
'These lines copy everything - including extra header rows
' and any SUBTOTAL formulas separated by blank row
'Uncomment them to use them
' myCell.Parent.Cells.SpecialCells(xlCellTypeVisible ).Copy
' mySht.Range("A1").PasteSpecial xlPasteValues


'These are the default - only copy the database values
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell

End Sub

"Keep It Simple Stupid" wrote in message
...
I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Group and Create New Sheets

How many sheets are you starting with?
Do you already have a sheet with the name of the 'group value' prior to running the macro?
What kinds of strings do you have in the 'group value' column? Can you manually rename a sheet with
those values, or do they have invalid characters?

HTH,
Bernie
MS Excel MVP


"Keep It Simple Stupid" wrote in message
...
I still can't get it to rename the new sheet as the value in Column B. I
don't know how to fix this. I do really need the "group value" as the sheet
name (whether or not it has a prefix). It always goofs when it gets to:

Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName

(And I have tried replacing myName with myCell.Value. It seems to work
when I put a specific name to the sheet like "Sheet 1", but as you can
imagine, it will not work when it gets around to creating the next sheet
because there is already a sheet named "Sheet 1")

Any ideas?

"Bernie Deitrick" wrote:

It might be the numbering.

Try changing:

myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value

to

myName = Worksheets(Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName

If that doesn't work, would adding a prefix to the number cause you problems?

If not, change the code to

myName = Worksheets("Sht " & Format(myCell.Value,"0")).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myName


HTH,
Bernie
MS Excel MVP


"Keep It Simple Stupid" wrote in message
...
An error message tells me that "Cannot rename a sheet to the same name as
another sheet, a referenced object library or a workbook reference by Visual
Basic"
Is it trying to create a new sheet for every row entry or did I forget to
change something?


"Bernie Deitrick" wrote:

K.I.S.S.,

Try the macro below, first selecting all your data and answering 2 when prompted.

HTH,
Bernie
MS Excel MVP


Sub ExportDatabaseToSeparateSheets()
'Export is based on the value in the desired column
Dim myCell As Range
Dim mySht As Worksheet
Dim myName As String
Dim myArea As Range
Dim myShtName As String
Dim KeyCol As Integer

myShtName = ActiveSheet.Name
KeyCol = InputBox("What column # within database to use as key?")


Set myArea = ActiveCell.CurrentRegion.Columns(KeyCol).Offset(1, 0).Cells

Set myArea = myArea.Resize(myArea.Rows.Count - 1, 1)

For Each myCell In myArea
On Error GoTo NoSheet
myName = Worksheets(myCell.Value).Name
GoTo SheetExists:
NoSheet:
Set mySht = Worksheets.Add(Befo=Worksheets(1))
mySht.Name = myCell.Value
With myCell.CurrentRegion
.AutoFilter Field:=KeyCol, Criteria1:=myCell.Value
'These lines copy everything - including extra header rows
' and any SUBTOTAL formulas separated by blank row
'Uncomment them to use them
' myCell.Parent.Cells.SpecialCells(xlCellTypeVisible ).Copy
' mySht.Range("A1").PasteSpecial xlPasteValues


'These are the default - only copy the database values
.SpecialCells(xlCellTypeVisible).Copy _
mySht.Range("A1")
mySht.Cells.EntireColumn.AutoFit
.AutoFilter
End With
Resume
SheetExists:
Next myCell

End Sub

"Keep It Simple Stupid" wrote in message
...
I am going to have multiple values in column B (they will not be the same
every time) that look like the following

A B C....
Apple 328 ...
Orange 427 ...
Pear 328 ...
Grape 519 ...
Banana 427 ...

I will need the unique values (and the entire row) to create a new sheet
(i.e. Apple 328 and Pear 328 should create a new sheet named "328")

As I said, the numbers that create the groups in Column B will be different
every time.








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
Name a group of sheets Mary Ann Excel Discussion (Misc queries) 2 November 26th 09 10:59 PM
working with a group of sheets GKeramidas Excel Programming 4 September 4th 06 05:05 PM
Multiple Sheets (Need to create 500 individual sheets in one workbook, pulling DATA Amaxwell Excel Worksheet Functions 4 August 17th 06 06:23 AM
Group Sheets Brian Keanie Excel Discussion (Misc queries) 10 January 1st 05 08:34 PM
Need group all sheets in a workbook TOMB Excel Programming 2 November 16th 04 03:58 PM


All times are GMT +1. The time now is 01:03 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"