#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 13
Default variable sheet name

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop



  #2   Report Post  
Posted to microsoft.public.excel.misc
KC KC is offline
external usenet poster
 
Posts: 94
Default variable sheet name

Try it this way

Set wsFrom = Sheets("Menu")
wsFrom.Cells(w,19).Value

hope this helps?

regards,
-kc
click YES if this helps.

"Gisela" wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 13
Default variable sheet name

The Menu sheet cell (w,19) has the name of the worksheet to be used later.
How can I do that? Is it possible?


"KC" wrote:

Try it this way

Set wsFrom = Sheets("Menu")
wsFrom.Cells(w,19).Value

hope this helps?

regards,
-kc
click YES if this helps.

"Gisela" wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop



  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 7,247
Default variable sheet name

Assume your sheet names are in column S of Sheet1, then you can use
code like the following:

Dim R As Range
Dim WS As Worksheet
Set R = Worksheets("Sheet1").Range("S1")
Do Until R.Text = vbNullString
Set WS = Worksheets(R.Text)
' do something with WS
Debug.Print WS.Name
Set R = R(2, 1)
Loop


Here, R is initialized to S1 on Sheet1. The code then loops, and sets
the WS variable to the Worksheet whose name is in the cells on column
S. Your code can then do whatever it needs to do with WS, and then the
loop continues reading column S on Sheet1 to get the subsequent sheet
names. It terminates when an empty cell is encountered in column S of
Sheet1.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Thu, 10 Sep 2009 13:15:26 -0700, Gisela
wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop



  #5   Report Post  
Posted to microsoft.public.excel.misc
KC KC is offline
external usenet poster
 
Posts: 94
Default variable sheet name

Set wsFrom = Sheets(Sheets("Menu").Cells(w, 19).Value)
wsFrom.Cells(x,y).value.......

would this help?
-kc

"Gisela" wrote:

The Menu sheet cell (w,19) has the name of the worksheet to be used later.
How can I do that? Is it possible?


"KC" wrote:

Try it this way

Set wsFrom = Sheets("Menu")
wsFrom.Cells(w,19).Value

hope this helps?

regards,
-kc
click YES if this helps.

"Gisela" wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop





  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 13
Default variable sheet name

Chip:

The row number where the worksheet name is, varies depending on the amount
of products. Thats why I need to have a variable to indicate which row needs
to be accessed. In this case I'm using w to determine the row number. Is it
possible to do that?

Set wsFrom = Sheets("Menu").Cells(w, 19).Value


"Chip Pearson" wrote:

Assume your sheet names are in column S of Sheet1, then you can use
code like the following:

Dim R As Range
Dim WS As Worksheet
Set R = Worksheets("Sheet1").Range("S1")
Do Until R.Text = vbNullString
Set WS = Worksheets(R.Text)
' do something with WS
Debug.Print WS.Name
Set R = R(2, 1)
Loop


Here, R is initialized to S1 on Sheet1. The code then loops, and sets
the WS variable to the Worksheet whose name is in the cells on column
S. Your code can then do whatever it needs to do with WS, and then the
loop continues reading column S on Sheet1 to get the subsequent sheet
names. It terminates when an empty cell is encountered in column S of
Sheet1.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Thu, 10 Sep 2009 13:15:26 -0700, Gisela
wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop




  #7   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 13
Default variable sheet name

I found the problem. I was defining wsfrom as a worksheet instead of a
String. My code is working.

Thanks anyway!!

"Chip Pearson" wrote:

Assume your sheet names are in column S of Sheet1, then you can use
code like the following:

Dim R As Range
Dim WS As Worksheet
Set R = Worksheets("Sheet1").Range("S1")
Do Until R.Text = vbNullString
Set WS = Worksheets(R.Text)
' do something with WS
Debug.Print WS.Name
Set R = R(2, 1)
Loop


Here, R is initialized to S1 on Sheet1. The code then loops, and sets
the WS variable to the Worksheet whose name is in the cells on column
S. Your code can then do whatever it needs to do with WS, and then the
loop continues reading column S on Sheet1 to get the subsequent sheet
names. It terminates when an empty cell is encountered in column S of
Sheet1.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Thu, 10 Sep 2009 13:15:26 -0700, Gisela
wrote:

Is there a way to refer to use a variable as the sheet name in a macro? I
getting run time error 424 he

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

For example, let's say that in columns S, I've got the names of sheets that
I want to refer to...

Column S
Sheet7
Sheet8
Sheet9
Sheet10

Dim wsFrom As Worksheet

I want to move the cell value to:

Set wsFrom = Sheets("Menu").Cells(w, 19).Value

whe w = 1 <---row 1 of column S; it increases by one until the product
changes

The purpose is to move certain data to a different worksheets (some code is
included)
Do Until IsEmpty(Sheets(wsFrom).Cells(x, 2))
'This will move the value of each column within each row to Data
worksheet
Do Until y = 19
Sheets("Data").Cells(x, y).Value = Sheets(wsFrom).Cells(x, y).Value
'increase the value of y by 1 to act on the next column
y = y + 1
Loop
'increase the value of x by 1 to act on the next row on Data worksheet
x = x + 1
'sets the value of y to 2 to act on the first column to move next row data
y = 2
Loop




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
Hide a sheet with a variable name Scott Excel Worksheet Functions 4 July 11th 08 08:21 AM
How To make a sheet reference Variable (eq: sum(sheet!D2:H2)) John Linker Excel Discussion (Misc queries) 3 June 16th 08 11:29 PM
Variable sheet and Formula Sheet Emmett423 Excel Discussion (Misc queries) 2 March 31st 06 02:14 AM
variable in a link where the variable is the name of the sheet darrelly Excel Worksheet Functions 1 October 7th 05 08:24 AM
Variable Sheet Name in Formula Barb R. Excel Discussion (Misc queries) 5 July 6th 05 06:20 PM


All times are GMT +1. The time now is 09:15 AM.

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"