Rename all existing worksheet tabs

G

Guest

I would like to take an existing workbook and rename all the worksheet tabs
at one time. For example, I might have ten worksheets with various names and
I'd like them all to be named USA1, USA2, USA3 and so on.

Can this be easily done with some VBA code? (I've seen some similar
questions, but none exactly like this one.)

Thanks.
 
B

Bob Phillips

Const sBase as string = "USA"
Dim i as long
Dim sh As Object

For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
G

Guest

Bob:
Thanks so much for your quick reply! I tried it and it works perfectly.

Could it be set up so I could define the worksheet name before running the
macro? (by typing it into a cell or something like that)?

For example, one workbook might need to be USA1, USA2, etc. and another
might need to be CANADA1, CANADA2, etc.
 
B

Bob Phillips

Mike,

This will get it from A1 on Sheet1, change to suit

Dim sBase as string
Dim i as long
Dim sh As Object

sBase = Worksheets("Sheet1").Range("A1").Value
For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh



--

HTH

RP
(remove nothere from the email address if mailing direct)
 
D

David McRitchie

Or more likely set up the prefix with an InputBox

sBase = Application.InputBox("Supply Prefix for worksheet renaming", _
"Rename worksheets", "USA")
If sBase = "" Then
MsgBox "Cancelled by your command"
exit sub
end if



Bob Phillips said:
Mike,

This will get it from A1 on Sheet1, change to suit

Dim sBase as string
Dim i as long
Dim sh As Object

sBase = Worksheets("Sheet1").Range("A1").Value
For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh



--

HTH

RP
(remove nothere from the email address if mailing direct)


MikeM said:
Bob:
Thanks so much for your quick reply! I tried it and it works perfectly.

Could it be set up so I could define the worksheet name before running the
macro? (by typing it into a cell or something like that)?

For example, one workbook might need to be USA1, USA2, etc. and another
might need to be CANADA1, CANADA2, etc.

Bob Phillips said:
Const sBase as string = "USA"
Dim i as long
Dim sh As Object

For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh

--

HTH

RP
(remove nothere from the email address if mailing direct)


"MikeM" <michael[dot]mittelmanl[at]db[dot]com> wrote in message
I would like to take an existing workbook and rename all the worksheet
tabs
at one time. For example, I might have ten worksheets with various names
and
I'd like them all to be named USA1, USA2, USA3 and so on.

Can this be easily done with some VBA code? (I've seen some similar
questions, but none exactly like this one.)

Thanks.
 
B

Bob Phillips

Don't like InputBox Dave, would much rather type in a cell. Too easy to make
a mistake, maybe not with USA, but easy with Kazakhstan.

--

HTH

RP
(remove nothere from the email address if mailing direct)


David McRitchie said:
Or more likely set up the prefix with an InputBox

sBase = Application.InputBox("Supply Prefix for worksheet renaming", _
"Rename worksheets", "USA")
If sBase = "" Then
MsgBox "Cancelled by your command"
exit sub
end if



Mike,

This will get it from A1 on Sheet1, change to suit

Dim sBase as string
Dim i as long
Dim sh As Object

sBase = Worksheets("Sheet1").Range("A1").Value
For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh



--

HTH

RP
(remove nothere from the email address if mailing direct)


MikeM said:
Bob:
Thanks so much for your quick reply! I tried it and it works perfectly.

Could it be set up so I could define the worksheet name before running the
macro? (by typing it into a cell or something like that)?

For example, one workbook might need to be USA1, USA2, etc. and another
might need to be CANADA1, CANADA2, etc.

:

Const sBase as string = "USA"
Dim i as long
Dim sh As Object

For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh

--

HTH

RP
(remove nothere from the email address if mailing direct)


"MikeM" <michael[dot]mittelmanl[at]db[dot]com> wrote in message
I would like to take an existing workbook and rename all the worksheet
tabs
at one time. For example, I might have ten worksheets with
various
names
and
I'd like them all to be named USA1, USA2, USA3 and so on.

Can this be easily done with some VBA code? (I've seen some similar
questions, but none exactly like this one.)

Thanks.
 
G

Guest

Many thanks to you Bob, and David as well for such quick and informative
responses. This is the first time I've posted a question here and will
certainly continue to do so in the future!
Mike

Bob Phillips said:
Don't like InputBox Dave, would much rather type in a cell. Too easy to make
a mistake, maybe not with USA, but easy with Kazakhstan.

--

HTH

RP
(remove nothere from the email address if mailing direct)


David McRitchie said:
Or more likely set up the prefix with an InputBox

sBase = Application.InputBox("Supply Prefix for worksheet renaming", _
"Rename worksheets", "USA")
If sBase = "" Then
MsgBox "Cancelled by your command"
exit sub
end if



Mike,

This will get it from A1 on Sheet1, change to suit

Dim sBase as string
Dim i as long
Dim sh As Object

sBase = Worksheets("Sheet1").Range("A1").Value
For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh



--

HTH

RP
(remove nothere from the email address if mailing direct)


"MikeM" <michael[dot]mittelmanl[at]db[dot]com> wrote in message
Bob:
Thanks so much for your quick reply! I tried it and it works perfectly.

Could it be set up so I could define the worksheet name before running the
macro? (by typing it into a cell or something like that)?

For example, one workbook might need to be USA1, USA2, etc. and another
might need to be CANADA1, CANADA2, etc.

:

Const sBase as string = "USA"
Dim i as long
Dim sh As Object

For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh

--

HTH

RP
(remove nothere from the email address if mailing direct)


"MikeM" <michael[dot]mittelmanl[at]db[dot]com> wrote in message
I would like to take an existing workbook and rename all the worksheet
tabs
at one time. For example, I might have ten worksheets with various
names
and
I'd like them all to be named USA1, USA2, USA3 and so on.

Can this be easily done with some VBA code? (I've seen some similar
questions, but none exactly like this one.)

Thanks.
 
G

Guest

David:
I tried this solution and it works nicely as well. Many thanks!
Mike

David McRitchie said:
Or more likely set up the prefix with an InputBox

sBase = Application.InputBox("Supply Prefix for worksheet renaming", _
"Rename worksheets", "USA")
If sBase = "" Then
MsgBox "Cancelled by your command"
exit sub
end if



Bob Phillips said:
Mike,

This will get it from A1 on Sheet1, change to suit

Dim sBase as string
Dim i as long
Dim sh As Object

sBase = Worksheets("Sheet1").Range("A1").Value
For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh



--

HTH

RP
(remove nothere from the email address if mailing direct)


MikeM said:
Bob:
Thanks so much for your quick reply! I tried it and it works perfectly.

Could it be set up so I could define the worksheet name before running the
macro? (by typing it into a cell or something like that)?

For example, one workbook might need to be USA1, USA2, etc. and another
might need to be CANADA1, CANADA2, etc.

:

Const sBase as string = "USA"
Dim i as long
Dim sh As Object

For Each sh In Activeworkbook.Sheets
i = i + 1
sh.name = sBase & i
Next sh

--

HTH

RP
(remove nothere from the email address if mailing direct)


"MikeM" <michael[dot]mittelmanl[at]db[dot]com> wrote in message
I would like to take an existing workbook and rename all the worksheet
tabs
at one time. For example, I might have ten worksheets with various names
and
I'd like them all to be named USA1, USA2, USA3 and so on.

Can this be easily done with some VBA code? (I've seen some similar
questions, but none exactly like this one.)

Thanks.
 
G

Guest

I would like to rename just selected worksheets. Could a modification of this
code be possible?
 
B

Bob Phillips

Const sBase As String = "USA"
Dim i As Long
Dim sh As Object

For Each sh In ActiveWorkbook.Windows(1).SelectedSheets
i = i + 1
sh.Name = sBase & i
Next sh

--
HTH

Bob Phillips

(replace somewhere in email address with googlemail if mailing direct)
 
G

Guest

Hello:

I would like to do this also. Is it possible to do without using VBA, but
simply a command in Excel?
 
D

Dave Peterson

There's not in Excel's user interface that would allow you do to lots of renames
all at once. You'd need a macro or do it manually (over and over).
 
G

Guest

I have a similar need, but wish to rename the tabs by referencing the same
cell on each worksheet that contains an invoice number.
 
D

Dave Peterson

One way:

Option Explicit
Sub testme01()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
With wks
On Error Resume Next
.Name = .Range("a1").Text
If Err.Number <> 0 Then
MsgBox .Name & " was not renamed"
Err.Clear
End If
On Error GoTo 0
End With
Next wks
End Sub
 
G

Guest

WOW! It's magic. Thank You Dave

Dave Peterson said:
One way:

Option Explicit
Sub testme01()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
With wks
On Error Resume Next
.Name = .Range("a1").Text
If Err.Number <> 0 Then
MsgBox .Name & " was not renamed"
Err.Clear
End If
On Error GoTo 0
End With
Next wks
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top