Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.worksheetfunctions
external usenet poster
 
Posts: 2
Default renaming worksheets with a macro

I have a set of workbooks with names of the form
Dataset_UV01_21_3327.xls and the individual worksheets are called
UV??_??_????_0

....

UV??_??_????_8

I wish to write a macro that will rename each sheet

sheet0 ... sheet8

So that other macros I write which move data from sheet to sheet will
work in any workbook.

I started with:

Worksheets("UV??_??_????_0").Name = "0"
Worksheets("UV??_??_????_1").Select
Worksheets("UV??_??_????_1").Name = "1"
Worksheets("UV??_??_????_2").Select
Worksheets("UV??_??_????_2").Name = "2"
Worksheets("UV??_??_????_3").Select
Worksheets("UV??_??_????_3").Name = "3"
...

And got Run-time error '9' Subscript out of range

I then tried like statements e.g.

If Sheet.Name Like "UV??_??_????_3" Then
Sheet.Name = "3"
End If

And got Run-time error '424': Object required

But I don't know how to fix these errors.

thanks in advance

Mike Cushman
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default renaming worksheets with a macro


The errors were caused because you cannot use wildcards like that.

Worksheets("UV??_??_????_0") would be looking for a worksheet with the
name UV??_??_????_0 which doesn't exist.

Not sure why the like didn't work...

Here's code to do what you want:

Sub renameum()

Dim s As Worksheet

For Each s In Worksheets
If InStrRev(s.Name, "_") 0 Then
s.Name = "Sheet" & Right(s.Name, Len(s.Name) - InStrRev(s.Name,
"_"))
End If
Next

End Sub

K


--
kkknie
------------------------------------------------------------------------
kkknie's Profile: http://www.excelforum.com/member.php...fo&userid=7543
View this thread: http://www.excelforum.com/showthread...hreadid=265554

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 92
Default renaming worksheets with a macro

As long as the sheet numbers stay single digit (i.e. 0-9) then how about
looping through the set

For Each Sheet In Sheets
Sheet.Name = Right((Sheet.Name), 1)
Next Sheet

If you have other sheets in the set you don't want renamed, then an If
statement could be used to check for conformance
Paul D

"btb_London" wrote in message
om...
I have a set of workbooks with names of the form
Dataset_UV01_21_3327.xls and the individual worksheets are called
UV??_??_????_0

...

UV??_??_????_8

I wish to write a macro that will rename each sheet

sheet0 ... sheet8

So that other macros I write which move data from sheet to sheet will
work in any workbook.

I started with:

Worksheets("UV??_??_????_0").Name = "0"
Worksheets("UV??_??_????_1").Select
Worksheets("UV??_??_????_1").Name = "1"
Worksheets("UV??_??_????_2").Select
Worksheets("UV??_??_????_2").Name = "2"
Worksheets("UV??_??_????_3").Select
Worksheets("UV??_??_????_3").Name = "3"
...

And got Run-time error '9' Subscript out of range

I then tried like statements e.g.

If Sheet.Name Like "UV??_??_????_3" Then
Sheet.Name = "3"
End If

And got Run-time error '424': Object required

But I don't know how to fix these errors.

thanks in advance

Mike Cushman



  #4   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.excel.worksheetfunctions
external usenet poster
 
Posts: 11
Default renaming worksheets with a macro

Define objects first.

Option Explicit
Public Sub renSheets()
Dim wb As Workbook, x, y As Integer
Set wb = ThisWorkbook
x = wb.Sheets.Count
For y = 1 To x Step 1
MsgBox wb.Sheets(y).Name
Next
End Sub


"btb_London" schreef in bericht
om...
I have a set of workbooks with names of the form
Dataset_UV01_21_3327.xls and the individual worksheets are called
UV??_??_????_0

...

UV??_??_????_8

I wish to write a macro that will rename each sheet

sheet0 ... sheet8

So that other macros I write which move data from sheet to sheet will
work in any workbook.

I started with:

Worksheets("UV??_??_????_0").Name = "0"
Worksheets("UV??_??_????_1").Select
Worksheets("UV??_??_????_1").Name = "1"
Worksheets("UV??_??_????_2").Select
Worksheets("UV??_??_????_2").Name = "2"
Worksheets("UV??_??_????_3").Select
Worksheets("UV??_??_????_3").Name = "3"
...

And got Run-time error '9' Subscript out of range

I then tried like statements e.g.

If Sheet.Name Like "UV??_??_????_3" Then
Sheet.Name = "3"
End If

And got Run-time error '424': Object required

But I don't know how to fix these errors.

thanks in advance

Mike Cushman



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default renaming worksheets with a macro

Thanks, this worked a dream (once I'd got rid of the line break!)

Mike

kkknie wrote in message ...
The errors were caused because you cannot use wildcards like that.

Worksheets("UV??_??_????_0") would be looking for a worksheet with the
name UV??_??_????_0 which doesn't exist.

Not sure why the like didn't work...

Here's code to do what you want:

Sub renameum()

Dim s As Worksheet

For Each s In Worksheets
If InStrRev(s.Name, "_") 0 Then
s.Name = "Sheet" & Right(s.Name, Len(s.Name) - InStrRev(s.Name,
"_"))
End If
Next

End Sub

K

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
Macro for renaming worksheets Joe[_9_] New Users to Excel 4 January 19th 08 06:08 PM
Renaming worksheets Mike Allen Excel Discussion (Misc queries) 8 January 21st 07 02:15 AM
Renaming Worksheets Steve Walford Excel Worksheet Functions 3 April 1st 05 09:29 PM
Renaming Worksheets rbanks[_10_] Excel Programming 2 June 16th 04 11:51 PM
Renaming worksheets! aiyer[_3_] Excel Programming 3 February 20th 04 12:09 AM


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