Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for Creating Worksheets in Excel

Status
Not open for further replies.

nobull613

Technical User
Jun 6, 2003
76
0
0
US
Is there a way to use VB code in Excel (version 2002) to automatically create worksheets and rename them to a list of values in another worksheet.

I would have a list of employee numbers in one worksheet and worksheet that is a template, that would use the specific employee number to do a variety of vlookup functions.
I would like the code to loop through the list of numbers and for each number:
1- copy the template worksheet
2- move it to the end of the workbook
3- rename the worksheet to the employee's number
Repeat until the end of the list of numbers is reached.

Thank you for your assistance.
 
First, a warning: Like data should be stored together.

That is, your life will be much easier in the long run if information for all employees is stored together on a single sheet. You can then use formulas on each of the employee-named sheets to pull information for that specific employee into that sheet.

This is opposed to the infinitely more cumbersome method of storing data for each employee on their own sheet and then trying to marry it all together on a summary sheet.

That having been said, yes, you can do what you asked.

Assuming you have a header row and your Employee IDs start in row 2 of Sheet 1; and assuming that your "Template" sheet is the second sheet in the workbook:


Code:
    '   Total Rows And Columns available in this version of Excel
    '**********************************************************
    dblAvailRows = ActiveSheet.Rows.Count
    dblAvailCols = ActiveSheet.Columns.Count

    '   Find last row
    '**********************************************************
    intLstRow = Sheets(1).Cells.Find( _
            What:="*", _
            After:=Cells(dblAvailRows, dblAvailCols), _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious).Row

    '   Loop through employee IDs, copy first sheet and rename
    '   for each ID
    '**********************************************************
    For i = 2 To intLstRow
        strTmpName = Sheets(1).Cells(i, 1).Value
        Sheets(2).Copy After:=Sheets(ActiveWorkbook.Sheets.Count)
        ActiveSheet.Name = strTmpName
    Next i

[tt][blue]-John[/blue][/tt]
[tab][red]The plural of anecdote is not data[/red]

Help us help you. Please read FAQ 181-2886 before posting.
 


Hi,

Mega ditto to John's advice.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I'm not sure I explained it well the first time. John, you are right, the data is stored in one worksheet, with the template (and subsequent Employee worksheets) linked to that main data worksheet to display the individual employee's data.

I gave the code and try and it seems to work perfectly.
Thank you very much John.
 


Linked? If you mean by cell references, what a pain!

Have you tried using PivotTables to display individula employee data?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top