ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How do I change a variable name based on a sheet name (https://www.excelbanter.com/excel-programming/439450-how-do-i-change-variable-name-based-sheet-name.html)

The Avenger

How do I change a variable name based on a sheet name
 
For example, lets say I have a string variable named strSales, I want to
iterate through the sheets, so when I'm on sheet 1, it'll be strSales1, on
sheet 2 it'll be strSales2, etc.

Mike H

How do I change a variable name based on a sheet name
 
Hi,

Maybe this

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & x

'your code


myvar = "strSales"
Next
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

For example, lets say I have a string variable named strSales, I want to
iterate through the sheets, so when I'm on sheet 1, it'll be strSales1, on
sheet 2 it'll be strSales2, etc.


The Avenger[_2_]

How do I change a variable name based on a sheet name
 
I don't believe that will work. It, essentially, changes the myvar to the
value of myvar1, myvar2, etc. What I'm attempting to do is reference a
different variable, based on the sheet name. Sorry for not being clear
earlier.

If I'm on Sheets("George"), I want to reference variable strSalesGeorge, if
on Sheets("Fred"), I want to reference variable strSalesFred, etc.

"Mike H" wrote:

Hi,

Maybe this

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & x

'your code


myvar = "strSales"
Next
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

For example, lets say I have a string variable named strSales, I want to
iterate through the sheets, so when I'm on sheet 1, it'll be strSales1, on
sheet 2 it'll be strSales2, etc.


Mike H

How do I change a variable name based on a sheet name
 
Hi,

It will work perfectly based upon your original question. This name includes
the sheet name

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & Sheets(x).name

'your code
MsgBox myvar

myvar = "strSales"
Next
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

I don't believe that will work. It, essentially, changes the myvar to the
value of myvar1, myvar2, etc. What I'm attempting to do is reference a
different variable, based on the sheet name. Sorry for not being clear
earlier.

If I'm on Sheets("George"), I want to reference variable strSalesGeorge, if
on Sheets("Fred"), I want to reference variable strSalesFred, etc.

"Mike H" wrote:

Hi,

Maybe this

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & x

'your code


myvar = "strSales"
Next
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

For example, lets say I have a string variable named strSales, I want to
iterate through the sheets, so when I'm on sheet 1, it'll be strSales1, on
sheet 2 it'll be strSales2, etc.


The Avenger[_2_]

How do I change a variable name based on a sheet name
 
Cool, ok, thank you

"Mike H" wrote:

Hi,

It will work perfectly based upon your original question. This name includes
the sheet name

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & Sheets(x).name

'your code
MsgBox myvar

myvar = "strSales"
Next
End Sub
--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

I don't believe that will work. It, essentially, changes the myvar to the
value of myvar1, myvar2, etc. What I'm attempting to do is reference a
different variable, based on the sheet name. Sorry for not being clear
earlier.

If I'm on Sheets("George"), I want to reference variable strSalesGeorge, if
on Sheets("Fred"), I want to reference variable strSalesFred, etc.

"Mike H" wrote:

Hi,

Maybe this

Sub nn()
myvar = "strSales"
For x = 1 To Worksheets.Count
myvar = myvar & x

'your code


myvar = "strSales"
Next
End Sub

--
Mike

When competing hypotheses are otherwise equal, adopt the hypothesis that
introduces the fewest assumptions while still sufficiently answering the
question.


"The Avenger" wrote:

For example, lets say I have a string variable named strSales, I want to
iterate through the sheets, so when I'm on sheet 1, it'll be strSales1, on
sheet 2 it'll be strSales2, etc.



All times are GMT +1. The time now is 10:35 AM.

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