Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Rename Multiple Sheets - Help with Mr Dave Peterson's Code


Hello All,
I am using Office2003/Windows XP and Mr Dave Peterson helped with my
problem.. I need help to modify that macro


The following macro Renames Sheet according to the names in "Index"

I wish to change the following to include Column B also
Set myRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))

and to change the following to show the value in B1 downward in Cell M2
of each individual renamed Sheet

Worksheets(wksName).Range("b5").Value = myCell.Value

For example now the macro renames sheet for eg: Tom and then put the
Name Tom in B5. Now I have included the Emp No. in Column B of
'Index' and I wish to have the employee number in M2 on each
individual sheet

Can anybody help me....
Thanks in advance
And Wishing every one a Very Very Happy and Prosperous New Year

Rashid Khan

Option Explicit
Sub RenameSheet()
Dim myCell As Range
Dim myRng As Range
Dim wks As Worksheet
Dim iCtr As Long
Dim wksName As String
iCtr = 0
With Worksheets("Index") '<-- worksheet with list of names.
Set myRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
For Each myCell In myRng.Cells
iCtr = iCtr + 1
wksName = Format(iCtr, "00")
If WorksheetExists(wksName, ThisWorkbook) = False Then
MsgBox "Worksheet named: " & wksName _
& " doesn't exist!" & vbLf & myCell.Value & " not
added!"
Else
Worksheets(wksName).Range("b5").Value = myCell.Value
On Error Resume Next
Worksheets(wksName).Name = myCell.Value
If Err.Number < 0 Then
MsgBox "Couldn't rename: " & _
wksName & " to " & myCell.Value
Err.Clear
End If
On Error GoTo 0
End If
Next myCell
End With
End Sub

Function WorksheetExists(SheetName As String, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Rename Multiple Sheets - Help with Mr Dave Peterson's Code

This is the line that assigns the value in column A to B5:
Worksheets(wksName).Range("b5").Value = myCell.Value

Just add another (right under that line) that looks like:
Worksheets(wksName).Range("M2").Value = myCell.offset(0,1).Value

Or maybe even a few lines to preserve formatting:

with Worksheets(wksName).Range("m2")
.Value = myCell.offset(0,1).Value
.numberformat = "000000"
end with

(Change that format to what you need.)



wrote:

Hello All,
I am using Office2003/Windows XP and Mr Dave Peterson helped with my
problem.. I need help to modify that macro

The following macro Renames Sheet according to the names in "Index"

I wish to change the following to include Column B also
Set myRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))

and to change the following to show the value in B1 downward in Cell M2
of each individual renamed Sheet

Worksheets(wksName).Range("b5").Value = myCell.Value

For example now the macro renames sheet for eg: Tom and then put the
Name Tom in B5. Now I have included the Emp No. in Column B of
'Index' and I wish to have the employee number in M2 on each
individual sheet

Can anybody help me....
Thanks in advance
And Wishing every one a Very Very Happy and Prosperous New Year

Rashid Khan

Option Explicit
Sub RenameSheet()
Dim myCell As Range
Dim myRng As Range
Dim wks As Worksheet
Dim iCtr As Long
Dim wksName As String
iCtr = 0
With Worksheets("Index") '<-- worksheet with list of names.
Set myRng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp))
For Each myCell In myRng.Cells
iCtr = iCtr + 1
wksName = Format(iCtr, "00")
If WorksheetExists(wksName, ThisWorkbook) = False Then
MsgBox "Worksheet named: " & wksName _
& " doesn't exist!" & vbLf & myCell.Value & " not
added!"
Else
Worksheets(wksName).Range("b5").Value = myCell.Value
On Error Resume Next
Worksheets(wksName).Name = myCell.Value
If Err.Number < 0 Then
MsgBox "Couldn't rename: " & _
wksName & " to " & myCell.Value
Err.Clear
End If
On Error GoTo 0
End If
Next myCell
End With
End Sub

Function WorksheetExists(SheetName As String, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 39
Default Rename Multiple Sheets - Help with Mr Dave Peterson's Code

Thanks Dave,

As usual you are a great help.
Works like a charm

Rashid

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
How do I rename multiple sheets at one time in Excel? Navigator Excel Worksheet Functions 1 July 25th 06 07:51 PM
Rename multiple sheets al Excel Programming 3 September 30th 05 01:11 PM
Help with Mr. Peterson's Code.. Print serially from a Sheet prkhan56 Excel Programming 50 July 31st 05 01:18 PM
Help with Mr. Dave Peterson's Code for Consolidating Many Sheets to One Rashid Khan Excel Programming 2 July 31st 04 07:31 PM
Macro to Rename Multiple Sheets Alan Excel Programming 1 January 9th 04 04:38 PM


All times are GMT +1. The time now is 10:24 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"