Using the Insert Function Dialog Box. Microsoft Excel VBA Programming
Reklaam
Reklaam
Chapter 20: Creating Worksheet Functions — and Living to Tell about It
345
‘ Sort the array
For i = 1 To NonEmpty
For j = i + 1 To NonEmpty
If SortedData(i) > SortedData(j) Then
Temp = SortedData(j)
SortedData(j) = SortedData(i)
SortedData(i) = Temp
End If
Next j
Next i
‘ Transpose the array and return it
Sorted = Application.Transpose(SortedData)
End Function
Figure 20-4:
Using a custom function to return a sorted range.
The Sorted function starts by creating an array named SortedData. This array contains all the nonblank values in the argument range. Next, the SortedData array is sorted, using a bubble sort algorithm. Because the array is a horizontal array, it must be transposed before it is returned by the function.
The Sorted Function works with a range of any size, as long as it’s in a single column or row. If the unsorted data is in a row, your formula needs to use
Excel’s TRANSPOSE function to display the sorted data horizontally. For example:
=TRANSPOSE(Sorted(A16:L16))
Using the Insert Function Dialog Box
The Excel Insert Function dialog box is a handy tool that lets you choose a worksheet function from a list and prompts you for the function’s arguments.
And, as I note earlier in this chapter, your custom worksheet functions also
Laadi alla
Reklaam