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
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
16,414
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
16,414
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,318
Messages
5,600,923
Members
414,416
Latest member
Nobu

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