PC Review


Reply
Thread Tools Rate Thread

Clever way to swap sheet names

 
 
Robert Crandal
Guest
Posts: n/a
 
      31st Dec 2009
I'm looking for a clever or efficient or cool way to
swap the names of 2 sheets using just one function
or subroutine call.

Suppose my workbook contains one sheet that
is named "foo" and the other is named "foo2".
I would want my function call to swap the names
of both sheets. If the function is called again,
it should reverse the process.

Everybody here always seem to have better or
shorter methods than mine, so I would greatly
appreciate your ideas.

thank you everyone.


 
Reply With Quote
 
 
 
 
Bob Phillips
Guest
Posts: n/a
 
      31st Dec 2009
Not much you can do cleverly I would have thought, but here is one way

Public Sub Test()
Call SwapNames(Worksheets("Sheet2"), Worksheets("Sheet3"))
End Sub

Public Function SwapNames(sh1 As Worksheet, sh2 As Worksheet)
Dim tmp As String

tmp = sh1.Name
sh1.Name = sh2.Name & "_"
sh2.Name = tmp
sh1.Name = Left(sh1.Name, Len(sh1.Name) - 1)
End Function


---
HTH

Bob Phillips

"Robert Crandal" <(E-Mail Removed)> wrote in message
news:_C__m.2$(E-Mail Removed)...
> I'm looking for a clever or efficient or cool way to
> swap the names of 2 sheets using just one function
> or subroutine call.
>
> Suppose my workbook contains one sheet that
> is named "foo" and the other is named "foo2".
> I would want my function call to swap the names
> of both sheets. If the function is called again,
> it should reverse the process.
>
> Everybody here always seem to have better or
> shorter methods than mine, so I would greatly
> appreciate your ideas.
>
> thank you everyone.
>
>



 
Reply With Quote
 
Mike H
Guest
Posts: n/a
 
      31st Dec 2009
Hi,

Not to sure about clever or cool but this will toggle the names of the first
and second sheets in a workbook

Sub Foo_Foo2()
Set firstsheet = Sheets(1)
Set secondsheet = Sheets(2)
firstsheet.Name = firstsheet.Name & secondsheet.Name
secondsheet.Name = Mid(firstsheet.Name, 1, _
Len(firstsheet.Name) - Len(secondsheet.Name))
firstsheet.Name = Right(firstsheet.Name, _
Len(firstsheet.Name) - Len(secondsheet.Name))
End Sub

Mike

"Robert Crandal" wrote:

> I'm looking for a clever or efficient or cool way to
> swap the names of 2 sheets using just one function
> or subroutine call.
>
> Suppose my workbook contains one sheet that
> is named "foo" and the other is named "foo2".
> I would want my function call to swap the names
> of both sheets. If the function is called again,
> it should reverse the process.
>
> Everybody here always seem to have better or
> shorter methods than mine, so I would greatly
> appreciate your ideas.
>
> thank you everyone.
>
>
> .
>

 
Reply With Quote
 
B Lynn B
Guest
Posts: n/a
 
      31st Dec 2009
I thought it might also be handy to make it work for any two sheets you have
selected...

Sub NameSwap()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim nameholder As String

If ActiveWindow.SelectedSheets.Count = 2 Then
Set ws1 = ActiveWindow.SelectedSheets(1)
Set ws2 = ActiveWindow.SelectedSheets(2)
nameholder = ws1.Name
ws1.Name = ws2.Name & "_"
ws2.Name = nameholder
ws1.Name = Replace(ws1.Name, "_", "")
End If

End Sub

"Robert Crandal" wrote:

> I'm looking for a clever or efficient or cool way to
> swap the names of 2 sheets using just one function
> or subroutine call.
>
> Suppose my workbook contains one sheet that
> is named "foo" and the other is named "foo2".
> I would want my function call to swap the names
> of both sheets. If the function is called again,
> it should reverse the process.
>
> Everybody here always seem to have better or
> shorter methods than mine, so I would greatly
> appreciate your ideas.
>
> thank you everyone.
>
>
> .
>

 
Reply With Quote
 
Robert Crandal
Guest
Posts: n/a
 
      31st Dec 2009
Hi Bob. Do you know if there's a way that this swap function will
work without reordering the tab positions after the swap occurs??
I prefer that the tabs stay in place and only their names get
swapped.

thank you


"Bob Phillips" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Not much you can do cleverly I would have thought, but here is one way
>
> Public Sub Test()
> Call SwapNames(Worksheets("Sheet2"), Worksheets("Sheet3"))
> End Sub
>
> Public Function SwapNames(sh1 As Worksheet, sh2 As Worksheet)
> Dim tmp As String
>
> tmp = sh1.Name
> sh1.Name = sh2.Name & "_"
> sh2.Name = tmp
> sh1.Name = Left(sh1.Name, Len(sh1.Name) - 1)
> End Function
>
>


 
Reply With Quote
 
Robert Crandal
Guest
Posts: n/a
 
      31st Dec 2009
> Hi Bob. Do you know if there's a way that this swap function will
> work without reordering the tab positions after the swap occurs??
> I prefer that the tabs stay in place and only their names get
> swapped.
>


Please disregard the above question.... I thought the tabs were
changing order, when in reality just the names were changing.
I think I had a "duh" moment there, hahahaha!


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      1st Jan 2010
Phew! You had me confused when I read the first one. :-)

Bob


"Robert Crandal" <(E-Mail Removed)> wrote in message
news:SE8%m.1846$_(E-Mail Removed)...
>> Hi Bob. Do you know if there's a way that this swap function will
>> work without reordering the tab positions after the swap occurs??
>> I prefer that the tabs stay in place and only their names get
>> swapped.
>>

>
> Please disregard the above question.... I thought the tabs were
> changing order, when in reality just the names were changing.
> I think I had a "duh" moment there, hahahaha!
>
>



 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      1st Jan 2010

"B Lynn B" <(E-Mail Removed)> wrote in message
news:9CED5C2D-F400-4812-9590-(E-Mail Removed)...
>I thought it might also be handy to make it work for any two sheets you
>have
> selected...



Isn't that what they all do?


 
Reply With Quote
 
CellShocked
Guest
Posts: n/a
 
      20th Feb 2010
I am now interestingly confused by both of you.

Strange world. :-)



On Fri, 1 Jan 2010 11:51:52 -0000, "Bob Phillips"
<(E-Mail Removed)> wrote:

>Phew! You had me confused when I read the first one. :-)
>
>Bob
>
>
>"Robert Crandal" <(E-Mail Removed)> wrote in message
>news:SE8%m.1846$_(E-Mail Removed)...
>>> Hi Bob. Do you know if there's a way that this swap function will
>>> work without reordering the tab positions after the swap occurs??
>>> I prefer that the tabs stay in place and only their names get
>>> swapped.
>>>

>>
>> Please disregard the above question.... I thought the tabs were
>> changing order, when in reality just the names were changing.
>> I think I had a "duh" moment there, hahahaha!
>>
>>

>

 
Reply With Quote
 
CellShocked
Guest
Posts: n/a
 
      20th Feb 2010
On Fri, 1 Jan 2010 11:52:34 -0000, "Bob Phillips"
<(E-Mail Removed)> wrote:

>
>"B Lynn B" <(E-Mail Removed)> wrote in message
>news:9CED5C2D-F400-4812-9590-(E-Mail Removed)...
>>I thought it might also be handy to make it work for any two sheets you
>>have
>> selected...

>
>
>Isn't that what they all do?
>

Note that by "selected" she means that two of your sheets, or tabs need
to be "selected" or "active" as recognized by Excel itself.

The other methods make no mention of a need for anyone to highlight or
"select" two sheets first. That is... since you only gave two sheets.

Her job works on a workbook with several worksheets to "choose" from.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate sheet names from list, assign data to summary sheet. Jason.Alden.Benoit@gmail.com Microsoft Excel Programming 4 20th Jun 2007 09:17 PM
How do I swap a group of surnames to the front of first names? =?Utf-8?B?V2VuZHk=?= Microsoft Excel Misc 1 1st Jun 2007 05:07 AM
Retrieving Excel Sheet Names/Column Names Rohit Microsoft ADO .NET 6 6th Dec 2005 01:59 PM
return all worksheet tab names and chart sheet tab names in report - an example DataFreakFromUtah Microsoft Excel Programming 2 6th Oct 2004 08:09 PM
Swap field names in code Razor Microsoft Access Form Coding 1 16th Dec 2003 04:19 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:11 PM.