Can in Array Calculations replace my loop

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,

I'm not really sure if this is possible, or maybe it is, but more than I should be asking the forum for?
At a minimum, if its not doable, at lease someone can get the useful formulas for adjustable average and standard deviation from cells E4 and F4.

So,
I currently have a worksheet that takes measured values for a process and a residual, then it
  • Calculates a ratio of the two
  • An average of the ratio defined by a user input and
  • The standard deviation of that same average ratio given the user input.
Actually there are a lot of other calculations, but I'm trying to simplify so I can clearly state the task I'm looking at.

Right now I use a loop procedure to change the input value (E4 in mini-sheet) and record the outcomes which for 6 months of run data and about 1200 individual processes/residuals, among other things, takes a little while.
I am okay with arrays, but don't even know how to approach calculating only in array and not in the spreadsheet one at a time.

Goal: To do this all in array.

I think I can figure out how to do simple math +, -, /, x inside an array, but have no idea how to go about doing the formulas below.
  • The average calculation formula =AVERAGE(OFFSET(D4,0,0,-(MIN(ROW()-ROW($D$3)+1,$E$1)),1))
  • The standard deviation formula = IFERROR(STDEV.S(OFFSET(D4,0,0,-(MIN(ROW()-ROW($D$3)+1,$E$1)),1)),"")
  • You can see in the manual example what these are doing, basically allowing a calculation of average or standard deviation when the date approaches less than the user input #.
Thanks for any guidance you can provide, in particular to handling the formulas above when going from input array to output array.

Example mini-sheet showing the data and calculations:

ArrayWorking.xlsb
ABCDEFGHIJKL
1Input (x)4
2
3DateA_ProcessA_ResidualRatioAvg. RatioStD Avg Ratio
44474689.743.7423.9723.97 
54474599.813.7626.5125.241.80
6447444.743.571.3317.2713.87
74474350.183.6713.6916.3711.46
84474242.203.6511.5613.2710.35
94474125.703.557.248.455.46
104474010.683.592.988.874.76
114473948.783.6613.328.774.63
124473844.183.6512.098.914.75
134473743.313.6511.8610.064.77
144473615.863.604.4110.424.06
154473561.963.6916.8011.295.12
164473464.223.6917.3912.616.00
174473321.973.616.0911.176.88
184473257.243.6815.5613.965.30
194473171.393.7119.2614.575.85
204473014.323.593.9811.227.35
214472987.233.7423.3315.538.33
22447286.253.581.7512.0810.81
234472739.133.6410.749.959.70
24
25Desired result in-array only to then be either saved to external file or put in another sheet for record.
26DateA_ProcessA_ResidualRatioAvg. Ratio 4Avg. Ratio 6Avg. Ratio 8Avg. Ratio 10StD Avg Ratio 4StD Avg Ratio 6StD Avg Ratio 8StD Avg Ratio 10
274474689.743.7423.9723.9712.1512.7514.08    
284474599.813.7626.5125.2420.4117.2615.651.801.801.801.80
29447444.743.571.3317.2717.2712.8613.0913.8713.8713.8713.87
304474350.183.6713.6916.3716.3715.2514.4711.4611.4611.4611.46
314474242.203.6511.5613.2715.4115.4112.7910.3510.1610.1610.16
324474125.703.557.248.4514.0514.0513.585.469.689.689.68
334474010.683.592.988.8710.5512.4712.474.769.169.789.78
344473948.783.6613.328.778.3512.5712.574.635.359.069.06
354473844.183.6512.098.9110.1511.0912.524.754.207.818.47
364473743.313.6511.8610.069.849.2612.464.773.954.827.99
374473615.863.604.4110.428.659.6410.504.064.384.187.22
384473561.963.6916.8011.2910.2410.039.535.125.394.725.19
394473464.223.6917.3912.6112.6410.7611.136.004.675.394.84
404473321.973.616.0911.1711.4410.6210.376.885.345.514.99
414473257.243.6815.5613.9612.0212.1910.775.305.614.765.25
424473171.393.7119.2614.5713.2512.9311.975.856.335.385.71
434473014.323.593.9811.2213.1811.9212.087.356.456.265.54
444472987.233.7423.3315.5314.2713.3513.088.337.637.446.59
45447286.253.581.7512.0811.6613.0212.0410.818.917.947.51
464472739.133.6410.749.9512.4412.2611.939.708.537.827.52
Sheet1
Cell Formulas
RangeFormula
D4:D23,D27:D46D4=B4/C4
E4:E23E4=AVERAGE(OFFSET(D4,0,0,-(MIN(ROW()-ROW($D$3)+1,$E$1)),1))
F4:F23F4=IFERROR(STDEV.S(OFFSET(D4,0,0,-(MIN(ROW()-ROW($D$3)+1,$E$1)),1)),"")
A4,A27A4=TODAY()
A5:A23,A28:A46A5=A4-1
E27:E46E27=AVERAGE(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$3)+1,4)),1))
F27:F46F27=AVERAGE(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$3)+1,6)),1))
G27:G46G27=AVERAGE(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$3)+1,8)),1))
H27:H46H27=AVERAGE(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$3)+1,10)),1))
I27:I46I27=IFERROR(STDEV.S(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$26)+1,4)),1)),"")
J27:J46J27=IFERROR(STDEV.S(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$26)+1,6)),1)),"")
K27:K46K27=IFERROR(STDEV.S(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$26)+1,8)),1)),"")
L27:L46L27=IFERROR(STDEV.S(OFFSET(D27,0,0,-(MIN(ROW()-ROW($D$26)+1,10)),1)),"")
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I can't seem to work exactly what you formulae are doing but here is some simple code to work out the average and the standard deviation for values in D4 to d23:
VBA Code:
Sub test()
lastrow = 23
inarr = Range(Cells(1, 4), Cells(lastrow, 4)) ' pick up all of column D
avarr = Range(Cells(1, 11), Cells(lastrow, 11)) ' pick up all of column K for averages
stdarr = Range(Cells(1, 12), Cells(lastrow, 12)) ' pick up all of column L for stdev
sumr = 0
For i = 4 To lastrow
   sumr = sumr + inarr(i, 1)
   avarr(i, 1) = sumr / (i - 3)
   deltasq = 0
   For j = 4 To i
     deltasq = deltasq + (inarr(j, 1) - avarr(i, 1)) * (inarr(j, 1) - avarr(i, 1))
   Next j

   stdarr(i, 1) = Sqr(deltasq / (i - 3))

 Next i
Range(Cells(1, 11), Cells(lastrow, 11)) = avarr ' write all of column K for averages
Range(Cells(1, 12), Cells(lastrow, 12)) = stdarr ' write all of column L for stdev
 
End Sub
 
Upvote 0
Solution
I can't seem to work exactly what you formulae are doing but here is some simple code to work out the average and the standard deviation for values in D4 to d23:
VBA Code:
Sub test()
lastrow = 23
inarr = Range(Cells(1, 4), Cells(lastrow, 4)) ' pick up all of column D
avarr = Range(Cells(1, 11), Cells(lastrow, 11)) ' pick up all of column K for averages
stdarr = Range(Cells(1, 12), Cells(lastrow, 12)) ' pick up all of column L for stdev
sumr = 0
For i = 4 To lastrow
   sumr = sumr + inarr(i, 1)
   avarr(i, 1) = sumr / (i - 3)
   deltasq = 0
   For j = 4 To i
     deltasq = deltasq + (inarr(j, 1) - avarr(i, 1)) * (inarr(j, 1) - avarr(i, 1))
   Next j

   stdarr(i, 1) = Sqr(deltasq / (i - 3))

 Next i
Range(Cells(1, 11), Cells(lastrow, 11)) = avarr ' write all of column K for averages
Range(Cells(1, 12), Cells(lastrow, 12)) = stdarr ' write all of column L for stdev
 
End Sub
Hi offthelip, I think I can make this work for me. Thank you for posting this code.

Let me try it out in the morning and come back to here. One question, perhaps a dumb one...from a computation speed point if view, is it better to continue to work with one large array if it is getting bigger and bigger as long as it isn't bumping up against memory resources or is it better to work with smaller ones and occasionally write them out to a range?
 
Upvote 0
It is almost invariably best to minimise the number of times you read or write to/from the worksheet, so that means one huge array is best.
 
Upvote 0
It is almost invariably best to minimise the number of times you read or write to/from the worksheet, so that means one huge array is best.
Have been reading up on multi dimensional arrays so will try to do in one large array. Thanks for that advice and for the code for average and standard deviation. They both work nicely so I will keep building.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
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