VBA Formula and other worksheets

Ezguy4u

Active Member
Joined
Feb 10, 2010
Messages
336
Office Version
  1. 365
Platform
  1. Windows
The workbook contains 3 worksheets. They are labeled Data 1, 2 and 3. Data 1 worksheet has that formula generated by VBA. So based on the below example, the question I have is there any other way to reference the 2 worksheets Data 2 and Data 3. I want to reference those 2 worksheets dynamically. If I add a worksheet or delete a worksheet, I would like the program to adjust for this. I hope this is clear enough. If not let me know.

VBA Code:
Sub Formula1()
Range("A1").Select
    ActiveCell.FormulaR1C1 = "=SUM('Data 2:Data 3'!RC)"
    Range("A2").Select
End Sub

Formula Worksheet.xlsm
A
112
Data 1
Cell Formulas
RangeFormula
A1A1=SUM('Data 2:Data 3'!A1)
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
My suggestion would be to make a new sheet (called say 'Data 0') and position it at the left of 'Data 2' and also a new sheet (called say 'Data n') and position it just to the right of 'Data 3' and

- Use this instead (if you really need vba to add it?)

VBA Code:
Sub Formula1_v2()
  Sheets("Data 1").Range("A1").FormulaR1C1 = "=SUM('Data 0:Data n'!RC)"
End Sub

- Make sure that any new sheets that you want included in the sum are added between 'Data 0' and 'Data n' and then the formula will never need adjusting for the new sheets added (or deleted)

(If you want, for visual effect, you can hide 'Data 0' and 'Data n', but you would need to remember that they are there and ensure that new sheets are added between them still)
 
Upvote 0
Well, this would work and I am thinking about how I can set up my workbook to make it work but I was wondering if there are other alternatives.
 
Upvote 0
I was wondering if there are other alternatives.
Assuming that 'Data 1', where the formula is going, is the first sheet in the workbook and that there will always be at least one other worksheet, see if this - put into the 'Data 1' sheet's module does what you want.

VBA Code:
Private Sub Worksheet_Activate()
  Range("A1").FormulaR1C1 = "=SUM('" & Sheets(2).Name & ":" & Sheets(Sheets.Count).Name & "'!RC)"
End Sub
 
Upvote 0
Solution
Peter this works the best. This is exactly what I was looking for. I can name Data 1 sheet01 and it should be the first one. I like the fact I can name a worksheet anything and the program doesn't care. And there will always be more than 1 worksheet. This is the syntax I was looking for. Thank You.
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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