Loop through Range/Array - Vectorized Calculations (VBA UDF)

drpdrpdrp

New Member
Joined
Sep 9, 2021
Messages
22
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I understand that I can take a range of values and make a vectorized calculation on it by using {...} in plain excel (Method 2)
(Method 1) below is showing the simplest way of accomplishing what I need:

1. Take the values in a range/vector/array,
2. subtract a fixed value from each
3. save as a new vector/range/array
4. average the values in the new_vector, and return the single value result


How can I write a VBA UDF { MyFunction(range, val) } to use on G9 (Method3) ?

Book1
ABCDEFGH
1rngMethod1Method2Method3
212val:10"manual""array"VBA
3232
45413
56544
61255
73452
8335
975.275.2???
10
Sheet1
Cell Formulas
RangeFormula
E3:E8E3=A2-$C$2
E9E9=AVERAGE(E3:E8)
F9F9=AVERAGE($A$2:$A$7-$C$2)
Press CTRL+SHIFT+ENTER to enter array formulas.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi & welcome to MrExcel.
How about
VBA Code:
Function drpdrp(Rng As Range, Numb As Double) As Double
   drpdrp = Evaluate("average(" & Rng.Address & "-" & Numb & ")")
End Function
 
Upvote 0
Thanks for the warming welcoming and reply, @Fluff. :)

Your proposed solution works great. Since we are on the topic: what would be a general solution with a For Loop that is easier to understand and extend later ?
 
Upvote 0
How about
VBA Code:
Function drpdrp(Rng As Range, Numb As Double) As Double
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Rng.Value
   For i = 1 To UBound(Ary)
      Ary(i, 1) = Ary(i, 1) - Numb
   Next i
   drpdrp = Application.Average(Ary)
End Function
 
Upvote 0
Solution
How about
VBA Code:
Function drpdrp(Rng As Range, Numb As Double) As Double
   Dim Ary As Variant
   Dim i As Long
  
   Ary = Rng.Value
   For i = 1 To UBound(Ary)
      Ary(i, 1) = Ary(i, 1) - Numb
   Next i
   drpdrp = Application.Average(Ary)
End Function
Much appreciated, Fluff. I was struggling with the Ary(x,y) part.... not obvious to me why the 2nd dimension has to be taken into account if it´s a 1-D Array.
Just need to get used to VBA. How do I mark your answer as the correct/selected one ?
 
Upvote 0
It's not a 1d array, whenever you load an array from a range it will be a 2d array, regardless of the number of rows or columns.
How do I mark your answer as the correct/selected one ?
Just click the Tick symbol to the right of the relevant post.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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