In VBA, change Excel Formula by incorporating columns in calculation

KDP36

New Member
Joined
Jan 10, 2014
Messages
16
Hi all,

Currently I have a formula in a sheet that calculates variance from a row in a Pivot table, such as B20 =VAR(B5:BL5). As additional data gets loaded into a data tab where the Pivot table reads in from (automatically refreshed by using ThisWorkbook.RefreshAll), how do I increment the number of columns each time to add new cells into this variance formula. For example, my current logic is L2 = 'NewDate', then B20 =VAR(B5:BM5). As NewDate gets updated, the last column in the variance function in B20 will move from BL to BM, BN, ... etc.
How do I do this? I tried to write a loop to increment the count but failed. Please advise on what's the most efficient way to do this, could be either in VBA or else. Much appreciated!

Stan
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,035
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi Stan

Provided there are not data at the right of your values, since the function VAR ignores blank cells maybe you can simply use, say, =VAR(B5:ZZ5).

Hope this helps

M.
 

KDP36

New Member
Joined
Jan 10, 2014
Messages
16
Thanks Marcelo. Unfortunately there is since this is a Pivot table. The rightmost column is the aggregated value, hence I don't want to include that.
 

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,035
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
I suppose the rightmost column has a header in row 4, say, Grand Total or whatever. So maybe this works
=VAR(B5:INDEX(5:5,MATCH("Grand Total",4:4,0)-1))

M.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,682
Messages
5,838,787
Members
430,569
Latest member
sbardelli

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
Top