MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2002, 04:19 PM   #1
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

From
http://www.mrexcel.com/board/viewtop...c=4847&forum=2

Quote:
On 2002-04-16 12:26, Jay Petrulis wrote:
Quote:
On 2002-04-16 11:05, Cosmos75 wrote:
I have a Worksheet with names (Column A) and info/values to the right (Columns B to E).

Each names appears more than once.

Is there a macro that can copy each persons’ info to a auto-generated sheet named after the person?
Hi,

Short answer:

1. Get a list of unique names and add a sheet, then name it the person's name

a) advanced filter and cycle through the list

b) loop through the list of names and determine whether the sheet exists or not. If it does, go on, else add the sheet.

2. Cycle through the data list and add each item to the appropriate sheet. You will match the name in the cell and transfer the data to the relevant sheet.

These two can/should be separate routines as you will only have to add sheets initially and/or when new people are added.

Here is a function (adapted from John W. Power Programming book) which can be called to determine if a sheet exists or not.

--------------------
Public Function SheetExists(sheetname) As Boolean
Dim abc As Object
On Error Resume Next
Set abc = ActiveWorkbook.Sheets(sheetname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function
--------------------

This should get you started.

HTH,
Jay
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 04:21 PM   #2
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Here's what I have so far.

But it doesn't work totally right, I get the first name in my list twice, EVEN after using advanced filter. At least that's how it shows up after the macro runs. So my macro stops, and always give me an extra sheet that has no name (except the default one).

Sub CreateSheets()

'Makes sure that the status bar is visible
Application.DisplayStatusBar = True

'Enter message for status bar
Application.StatusBar = "Performing Task!! Please Wait!!"

'Turn off Screen updating
Application.ScreenUpdating = False

Range("A2", Range("A65536").End(xlUp).Address).Select
RowCounter = Selection.Count

NameRange = Range("A2", Range("A65536").End(xlUp).Address).Address


Range(NameRange).AdvancedFilter Action:=xlFilterInPlace, Unique:=True

Dim SheetName As String


For i = 2 To RowCounter + 1
Sheets("Names").Select
SheetName = Range("A" & i).Value
Sheets.Add
ActiveSheet.Select
ActiveSheet.Name = SheetName
Next i

ActiveSheet.ShowAllData



'Turn on Screen Updating
'(Happens automatically after a module is finished)
Application.ScreenUpdating = True

'Reset the Status Bar
Application.StatusBar = False

End Sub


[ This Message was edited by: Cosmos75 on 2002-04-16 15:37 ]
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 04:36 PM   #3
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hi,

Names on sheet "Names" beginning in A2 down the column.

Delete all sheets except for "Names" and run the Add_Names() macro below.

Then, run it again after finishing to test that duplicates sheets are not added.

Of course, save your work before doing all this.

------------------
Option Explicit
Public Function NameofSheet()
NameofSheet = Application.Caller.Parent.Name
End Function

Public Function SheetExists(sheetname) As Boolean
Dim abc As Object
On Error Resume Next
Set abc = ActiveWorkbook.Sheets(sheetname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
End Function

Sub Add_Sheets()
Dim lastrow As Long, sheettoname As String, x As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
lastrow = Sheets("Names").Cells(Rows.Count, 1).End(xlUp).Row

For x = 2 To lastrow
sheettoname = Sheets("Names").Cells(x, 1)
If SheetExists(sheettoname) = True Then
' do nothing
Else
Worksheets.Add After:=Sheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Sheets("Names").Cells(x, 1).Value
End If
Next x
Sheets("Names").Select
MsgBox "Done!"
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
----------------------

HTH,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 16th, 2002, 04:40 PM   #4
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Thanks, Jay!

Now to try and tackle populating those named sheets with their respective namesakes info....

Any ideas on why mine doesn't work so well?

[ This Message was edited by: Cosmos75 on 2002-04-16 15:40 ]
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 04:45 PM   #5
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

In your Advanced Filter, start the NameRange at A1, not A2, and you will remove the duplicate first name. You will get the header, but you don't cycle through that when you add/name the sheets, so you should be OK there.

If I see anything else, I'll let you know.

I think writing the data to the sheets should be fairly straightforward from here.

I will look to see if there is anything else I can add.

Bye,
Jay
Jay Petrulis is offline   Reply With Quote
Old Apr 16th, 2002, 05:01 PM   #6
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

The data should be OK.

Probably use something with the structure

If Cell.Value = ws.name Then
Sheets(ws.name).Cell("A1") = Cell.Value

use a loop or For i to ...

to go in and do it.

It'll be trial and error for me since I am not good at VBA. have to record macros and changes them use existing macros that I've learned.

THANKS!
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 05:09 PM   #7
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Got it to work! (with Help from Jay pointing out my mistake!) The creating sheets part, that is! Here's the code!

Sub CreateSheets()

'Makes sure that the status bar is visible
Application.DisplayStatusBar = True

'Enter message for status bar
Application.StatusBar = "Performing Task!! Please Wait!!"

'Turn off Screen updating
Application.ScreenUpdating = False




NameRange = Range("A1", Range("A65536").End(xlUp).Address).Address

Range(NameRange).AdvancedFilter Action:=xlFilterInPlace, Unique:=True



Range("A2", Range("A65536").End(xlUp).Address).Select
RowCounter = Selection.Count

Dim sheetname As String


For i = 2 To RowCounter + 1
Sheets("Names").Select
sheetname = Range("A" & i).Value
Sheets.Add
ActiveSheet.Select
ActiveSheet.Name = sheetname
Next i

Sheets("Names").Select

ActiveSheet.ShowAllData



'Turn on Screen Updating
'(Happens automatically after a module is finished)
Application.ScreenUpdating = True

'Reset the Status Bar
Application.StatusBar = False

End Sub
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 05:10 PM   #8
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Jay,

Would yours be faster than mine for a lot of sheets? Why would the faster one be the faster one? (hope that makes sense)

THANKS!
Cosmos75 is offline   Reply With Quote
Old Apr 16th, 2002, 05:25 PM   #9
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Hard to say. I am looping through the entire range in column A, whereas you are only working with the unique elements, so yours should be faster on that front.

You should check, though, that you don't keep adding sheets if the macro is run again, or worse, throw an error. Only add the sheet if it doesn't already exist.

Your code jumps back and forth between worksheets. Delete the following line

ActiveSheet.Select

as it is not necessary and would speed it ever so slightly.

The speed differences should be negligible in any case. Writing to the worksheets shouldn't be slow either.

The one advantage of my approach would be to do something like the following pseudo code:

Loop through the names
if the sheet doesn't exist then
1) add it
2) copy the data to the next available row

if the sheet exists then
1) copy the data to the next available row

Only one pass through the data.

Also, if your Names sheet is also your data sheet, consider doing the following:

1. Adding new sheets (already done)
2. Looping through with Autofilter to isolate the names and associated data and copying all of it in one shot.

This is dependent on how you want your data to fall on the new sheet but would be very fast. I have coded like this before, but it would take a bit to find it.

Bye,
Jay


Jay Petrulis is offline   Reply With Quote
Old Apr 16th, 2002, 06:34 PM   #10
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Jay,

First of all, THANK YOU for all you help!!

For i = 2 To RowCounter + 1
Sheets("Names").Select
sheetname = Range("A" & i).Value
Sheets.Add
ActiveSheet.Select
ActiveSheet.Name = sheetname
Next i

Wouldn'e taking out ActiveSheet.Select make the sheet Names be the active one (since there is Sheets("Names").Select or does adding a sheet make the added sheet the active sheet?

On my way home, I realiased that looping could take awhile. Proably better to use autofilter and copy the data with the filter on each name. Although I am not sure how to loop through the names. Maybe something like

For Each ws in Worksheet
Sheets("Names"),Select
'Autofilter using ws.name and select. (Not sure of the code, will have to wait till I I get to work and have the use of Excel.
'Then copy back into the respective sheet
ws.select.paste

[ This Message was edited by: Cosmos75 on 2002-04-16 17:37 ]
Cosmos75 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On

Forum Jump


All times are GMT -4. The time now is 09:21 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes