VBA variable Max function

TestableEmu263

New Member
Joined
May 19, 2022
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hey everyone, New to VBA and I want to figure out how to create a function that will take the maximum of a range that the user can define via a single cell reference. For example, If I have a list of 10 items and I want the maximum of the first 5 items, I can simply pick that range with the built in max function in excel. But if that list of items is variable and it changes to say 24 items and I instead want to take a max of the first 12 items, how can I go about doing that so that I don't have to manually change the range each time? Thanks for the help
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe:
Book1.xlsm
ABCD
1MyRangenumCellsMax
22334
3331237
4341848
526
618
715
81
937
100
1134
1225
1330
1420
1512
1612
1728
1824
1948
202
Sheet4
Cell Formulas
RangeFormula
D2:D4D2=MaxOfFirstN($A$2:$A$20,C2)

VBA Code:
Function MaxOfFirstN(R As Range, N As Long) As Double
MaxOfFirstN = Application.Max(Range(R.Cells(1, 1), R.Cells(N, 1)))
End Function
 
Upvote 0
Very Close, The only other thing I was trying to make the function do is take the max of the next group as well. So for your example, you posted above, the only change needed would be something that makes the function also able to be carried down into the next respective grouping.

Further info:
1) A list of x entries is input into a column. This list can be any number of items long.
2) this list can then be split into groups. For example, a list of 60 items can be split into 6 groups of 10 or 12 groups of 5.
3) Depending on what the user chooses whether that be the aforementioned 6 or 12 groups, the function then takes the max of each group

Thanks again for your previous reply... and in addition I'm very new to VBA and was wondering if you knew of any resources I could use to learn: Book, youtube channel, etc
 
Upvote 0
Very Close, The only other thing I was trying to make the function do is take the max of the next group as well. So for your example, you posted above, the only change needed would be something that makes the function also able to be carried down into the next respective grouping.

Further info:
1) A list of x entries is input into a column. This list can be any number of items long.
2) this list can then be split into groups. For example, a list of 60 items can be split into 6 groups of 10 or 12 groups of 5.
3) Depending on what the user chooses whether that be the aforementioned 6 or 12 groups, the function then takes the max of each group

Thanks again for your previous reply... and in addition I'm very new to VBA and was wondering if you knew of any resources I could use to learn: Book, youtube channel, etc
You are welcome - thanks for the reply. I don't see anything in your OP that alludes to a function that returns more than one value. I think a subroutine would be a better choice for the sort of additional information to be returned.
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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