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

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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.
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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