Mean of N largest values in a range of cells

Cloclo1990

New Member
Joined
May 21, 2019
Messages
4
  1. Dear All, I need your help in creating a VBA code.

    I must create a user-defined function to calculate the mean of the N largest values in a range of cells.


    It is compulsory I use this function: Function MeanOfLargest (DataRange as Range, NbVals as Integer) as Variant


    DataRange is a range of cells containing numbers; NbVals is the number of largest values in dataRange with which the mean will be calculated.

    Thank you for your help ! Have a great day
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Please do not expect us to complete your homework/assignments for you - that is not what this board is intended to do.

Your best bet is to post what you have done/tried so far, and we may be able to give you tips to point you in the right direction.
 
Upvote 0
I have done this, but it is not complete at all...



Option Explicit


Function MeanOfLargest(DataRange As Range, NbVals As Integer) As Variant


'Declare Variables
Dim NbVals As Integer, i As Integer


'Set Range from which to dertermine the largest Value
MsgBox "The Range is: " & Range("A1").End(xlDown).Row
Cells(1, 3).Value = Range("A1").End(xlDown).Row


'Ask User input
NbVals = InputBox("Enter a number of values from which the mean will be calculated: ")
Cells(2, 3).Value = NbVals


'Ensure Number Entered >= to 1


Do Until NbVals >= 1
MsgBox "ERROR: Parameter 2 out of Range"
NbVals = InputBox("Re-Enter a new number of values from which the Mean will be calculated: ")
Loop


'Ensure Number Entered <= Range
Do Until NbVals <= Cells(1, 3).Value
MsgBox "ERROR: Parameter 2 out of Range"
NbVals = InputBox("Re-Enter a new number of values from which the Mean will be calculated: ")
Loop


'Show N Largest Values in the array
For i = 1 To NbVals
Cells(1 + i, 5).Value = i
Do Until Cells(1 + i, 5).Value = ""
Cells(1 + i, 6).Value = "=LARGE (A:A,{1:i})" !!!!!!!??????
Loop
Next i




'Function Execution
MeanOfLargest = ...?


'Display Message Box with mean result to user
MsgBox "The Mean is: " & MeanOfLargest


End Function




'Program to Clear Data before new execution


Sub ClearSheet()


Dim i As Integer


Cells(1, 3).Value = ""
Cells(2, 3).Value = ""
Cells(2, 9).Value = ""


For i = 1 To 10000
Cells(1 + i, 5).Value = ""
Cells(1 + i, 6).Value = ""
Next i


End Sub



 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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