Consider the following User Defined Function:

```
Public Function ransum(HowMany As Long, Population As Range) As Long
Dim i As Long, N As Long, aray(), r As Range
N = Population.Count
ReDim aray(1 To N)
i = 1
For Each r In Population
aray(i) = r.Value
i = i + 1
Next r
Call Shuffle(aray)
ransum = 0
For i = 1 To HowMany
ransum = ransum + aray(i)
Next i
End Function
Public Sub Shuffle(InOut() As Variant)
Dim i As Long, J As Long
Dim tempF As Double, Temp As Variant
Hi = UBound(InOut)
Low = LBound(InOut)
ReDim Helper(Low To Hi) As Double
Randomize
For i = Low To Hi
Helper(i) = Rnd
Next i
J = (Hi - Low + 1) \ 2
Do While J > 0
For i = Low To Hi - J
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
Temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = Temp
End If
Next i
For i = Hi - J To Low Step -1
If Helper(i) > Helper(i + J) Then
tempF = Helper(i)
Helper(i) = Helper(i + J)
Helper(i + J) = tempF
Temp = InOut(i)
InOut(i) = InOut(i + J)
InOut(i + J) = Temp
End If
Next i
J = J \ 2
Loop
End Sub
```

So in *Sheet1* cell **B1** we enter:

=ransum(A1,Sheet2!A$1:A$100)

and copy down:

This assumes that the data to be sampled is in cells **A1** through **A100** of the second sheet.

User Defined Functions (UDFs) are very easy to install and use:

- ALT-F11 brings up the VBE window
- ALT-I
ALT-M opens a fresh module
- paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx

To remove the UDF:

- bring up the VBE window as above
- clear the code out
- close the VBE window

To use the UDF from Excel:

=myfunction(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

**Macros must be enabled for this to work!**