ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sub to strip away "Sheet" prefix from names (https://www.excelbanter.com/excel-programming/387557-sub-strip-away-sheet-prefix-names.html)

Max

Sub to strip away "Sheet" prefix from names
 
Looking for a sub which can strip away the "Sheet" prefix from all sheets
bearing such names, eg sheets named as: Sheet2, Sheet3, Sheet4 ... will be
renamed as simply: 2,3,4 ... Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---



Norman Jones

Sub to strip away "Sheet" prefix from names
 
Hi Max,

Try something like:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet

Set WB = Workbooks("MyBook.xls") '<<==== CHANGE

For Each SH In WB.Worksheets
With SH
If UCase(.Name) Like "SHEET*" Then
.Name = Replace(.Name, "Sheet", _
vbNullString, 1, 1, vbTextCompare)
End If
End With
Next SH
End Sub
'<<=============


---
Regards,
Norman


"Max" wrote in message
...
Looking for a sub which can strip away the "Sheet" prefix from all sheets
bearing such names, eg sheets named as: Sheet2, Sheet3, Sheet4 ... will be
renamed as simply: 2,3,4 ... Thanks.
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---




Max

Sub to strip away "Sheet" prefix from names
 
Norman,
Many thanks. Runs great !

Set WB = Workbooks("Test1a.xls") '<<==== CHANGE

Just one question, how could your sub be generalized
to run on all books open simultaneously in the same Excel session ?
(hopefully the filenames need not be specified)

Thanks
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---



Norman Jones

Sub to strip away "Sheet" prefix from names
 
Hi Max,

'------------------
Set WB = Workbooks("Test1a.xls") '<<==== CHANGE

Just one question, how could your sub be generalized
to run on all books open simultaneously in the same Excel session ?
(hopefully the filenames need not be specified)
'------------------

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet

For Each WB In Application.Workbooks
For Each SH In WB.Worksheets
With SH
If UCase(.Name) Like "SHEET*" Then
.Name = Replace(.Name, "Sheet", _
vbNullString, 1, 1, vbTextCompare)
End If
End With
Next SH
Next WB
End Sub
'<<=============



---
Regards,
Norman



Max

Sub to strip away "Sheet" prefix from names
 
Marvellous, Norman !
Thanks
--
Max
Singapore
http://savefile.com/projects/236895
xdemechanik
---




All times are GMT +1. The time now is 08:22 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com