Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Refering to a Sheet Name in a macro

I have a workbook with one sheet, that I will call the "master sheet". It
contains all my data. I am selecting a row from this master sheet, opening a
new sheet, and pasting my data from the master sheet to this new sheet.

I then take the value of a cell in the new sheet and use that value for the
sheet name. For example, a cell may contain the value '50'. So '50' becomes
the sheet name. I use this code:
Dim sheetName
sheetName = ActiveCell.Value
Sheets("Sheet1").Select
Sheets("Sheet1").Name = sheetName

This works. I then go back to the master sheet and select another row.
What I want to do then is to go back to the sheet I just created by using the
variable 'sheetName' (this name may change, that's why I want to use the
variable).

I tried using something like: Sheets 'sheetName'.Select or Sheets
(sheetName).Select. But it errors out because of sheetName. When I mouse
over sheetName in the macro it gives me the value of the new sheet. So I
know it has the correct value in it.

Is there a way of doing this? Thanks.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Refering to a Sheet Name in a macro

sheets(sheetname).Select

should work if sheetname is a variable that contains the name of a sheet in
the activeworkbook.

Here is a demonstration from the immediate window:
sheetname = "Sheet2"
? activesheet.Name
Sheet1 (2)
sheets(sheetname).Select
?Activesheet.Name
Sheet2

--
Regards,
Tom Ogilvy


"tedd13" wrote:

I have a workbook with one sheet, that I will call the "master sheet". It
contains all my data. I am selecting a row from this master sheet, opening a
new sheet, and pasting my data from the master sheet to this new sheet.

I then take the value of a cell in the new sheet and use that value for the
sheet name. For example, a cell may contain the value '50'. So '50' becomes
the sheet name. I use this code:
Dim sheetName
sheetName = ActiveCell.Value
Sheets("Sheet1").Select
Sheets("Sheet1").Name = sheetName

This works. I then go back to the master sheet and select another row.
What I want to do then is to go back to the sheet I just created by using the
variable 'sheetName' (this name may change, that's why I want to use the
variable).

I tried using something like: Sheets 'sheetName'.Select or Sheets
(sheetName).Select. But it errors out because of sheetName. When I mouse
over sheetName in the macro it gives me the value of the new sheet. So I
know it has the correct value in it.

Is there a way of doing this? Thanks.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Refering to a Sheet Name in a macro

a7n9 raises a good point. sheetname should be typed as string.

Sub efg()
Dim sheetname As String
sheetname = 50
Sheets(sheetname).Select

End Sub

works fine, but if you made sheetname a variant either by declaration or by
not declaring it at all (variant is the default), then it would contain the
number 50 and look for the 50th sheet in the sheet tab - which would raise an
error.

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote:

sheets(sheetname).Select

should work if sheetname is a variable that contains the name of a sheet in
the activeworkbook.

Here is a demonstration from the immediate window:
sheetname = "Sheet2"
? activesheet.Name
Sheet1 (2)
sheets(sheetname).Select
?Activesheet.Name
Sheet2

--
Regards,
Tom Ogilvy


"tedd13" wrote:

I have a workbook with one sheet, that I will call the "master sheet". It
contains all my data. I am selecting a row from this master sheet, opening a
new sheet, and pasting my data from the master sheet to this new sheet.

I then take the value of a cell in the new sheet and use that value for the
sheet name. For example, a cell may contain the value '50'. So '50' becomes
the sheet name. I use this code:
Dim sheetName
sheetName = ActiveCell.Value
Sheets("Sheet1").Select
Sheets("Sheet1").Name = sheetName

This works. I then go back to the master sheet and select another row.
What I want to do then is to go back to the sheet I just created by using the
variable 'sheetName' (this name may change, that's why I want to use the
variable).

I tried using something like: Sheets 'sheetName'.Select or Sheets
(sheetName).Select. But it errors out because of sheetName. When I mouse
over sheetName in the macro it gives me the value of the new sheet. So I
know it has the correct value in it.

Is there a way of doing this? Thanks.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Refering to a Sheet Name in a macro


Even though your sheetname variable has a number, it is not a number
when you refer it.

Try
Dim sheetName
sheetName = CStr(ActiveCell.Value)
Sheets("Sheet1").Name = sheetName

Also, you don't have to select a sheet to rename it, you can directly
do it.


--
a7n9


------------------------------------------------------------------------
a7n9's Profile: http://www.excelforum.com/member.php...o&userid=32149
View this thread: http://www.excelforum.com/showthread...hreadid=562894

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
Refering to a sheet in a cell Jarek Kujawa[_2_] Excel Discussion (Misc queries) 0 July 31st 08 11:06 AM
Refering to a sheet in a cell Jarek Kujawa[_2_] Excel Discussion (Misc queries) 0 July 31st 08 10:02 AM
REFERING TO CELL IN DIFFERENT SHEET cjbarron5 Excel Discussion (Misc queries) 2 May 28th 08 04:32 AM
refering to previous sheet [email protected] Excel Discussion (Misc queries) 3 January 25th 06 02:55 PM
Problem refering to Add-In sheet N E Body[_21_] Excel Programming 4 August 20th 05 06:34 PM


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