is it possible

MAMIBUSSOL

Board Regular
Joined
Jun 2, 2011
Messages
95
I have a variable called SALES1

I have a worksheet called SALES1 (this name could change)

normal coding would give me

SALES1 = "=SUM('SALES1'!B37)

the problem is how do I write coding which will cater for the worksheet changing name.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,

Not sure if this helps.

Code:
Sub Test()
Range("B38").Formula = "=SUM(" & ActiveSheet.Name & "!B37)"
End Sub

Biz
 
Upvote 0
thanks for respoonding Biz, it does help with the a possible solution, however I would still have the problem of not knowing the name of the sheet. Unless I misunderstood the code you gave me; I would still need to select the worksheet; and this could change. But you have helped with a the next phase which would have been how to do the calculation.
 
Upvote 0
Why not rename your variable (or the sheet name) so that they do not share the same name.

You could use any of the following and still have a variable and sheet "called" SALES1:
SALES1
SALES 1
SALES_1
SALES(1)
SALES01
var_SALES01
 
Upvote 0
my master file is made up of the following worksheets
VAT
SALES1 - this will get changed to a month and year ie APR11
SALES2 - ETC
SALES3 - ETC

the master file will get used to record the data, sales1 2 and 3 will get changed to a monthyear value depending on when its used.

my coding needs to be flexible to deal with this occurance

for example first time the file is used SALES1 might be called APR11
however next time I use the file SALES1 might be called JUL11, as it isn't practical to adjust the coding each time, It makes logical sense to make the coding flexible enough to cater for this problem
 
Upvote 0
There are two ways to do this.
One would be with Named Ranges.
If you have Name: myRange RefersTo:Sales1!$A$1, then the RefersTo will adjust as the user changes the name of the sheet (or the tab name changes via VBA).

The other way involves the use of code names.
In VBA you can refer to a sheet by its tab name
Sheets("Sales1").Range("A1")
or by its code name
Sheet1.Range("A1")

(more info at this link http://www.mrexcel.com/forum/showthread.php?t=253546&highlight=codename)

To write formulas
Code:
ActiveSheet.Range("A1") = "=SUM(" & Sheet1.Range("B37").Address(True,True,xlA1,True) & ")"
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,841
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top