我想在Excel中實現一個自定義函數,該函數返回修剪後的數據樣本的最小值。獲得「最小修剪」,與Excel相似TRIMMEAN功能
兩個輸入:
- 數據
- 百分比,其中規定從原始數據樣本多少個數據點應排除
我的第一個草案(如下所示),射門的兩個特徵現在:
- 當我使用fu並選擇一個整列(例如, G。 = TrimMIN(A:A)),需要花費很長的時間
- 我需要修剪它,但線「data.Cells.Sort」前輸入範圍「數據」進行排序不工作
展望在這兩個問題上得到一些想法。
我的代碼:
Function TrimMIN(data As Range, percentage As Double) As Double
Dim dataNew As Range
Dim dataNewS As Variant
Dim diff, counter, upper, lower, countDataNew As Double
counter = 0
'data.Cells.Sort
diff = Round(data.Count * percentage/2, [0])
Debug.Print "diff= " & diff
upper = data.Count - diff
lower = diff
countDataNew = data.Count - diff - diff
'Application.Min(data)
'Debug.Print "upper= " & upper
'Debug.Print "lower= " & lower
'Debug.Print "data.count= " & data.count
'Debug.Print "countDataNew= " & countDataNew
Dim cel As Range
For Each cel In data.Cells
counter = counter + 1
'Debug.Print "counter= " & counter
Debug.Print "celValue= " & cel.Value
If counter > lower And counter <= upper Then
'Debug.Print "counter in range, counter is " & counter
If Not dataNew Is Nothing Then
' Add the 2nd, 3rd, 4th etc cell to our new range, rng2
' this is the most common outcome so place it first in the IF test (faster coding)
Set dataNew = Union(dataNew, cel)
Else
' the first valid cell becomes rng2
Set dataNew = cel
End If
End If
Next cel
'Debug.Print "dataNew.count " & dataNew.count
TrimMIN = Application.Min(dataNew)
End Function
_「我需要修整之前將其輸入範圍‘數據’排序」 _:是什麼樣的排序你需要嗎? 「數據」範圍的_結構是什麼 – user3598756
上面顯示的代碼將數據範圍修剪爲較小的一個。當數據已經被排序時,計算MIN是很好的(例如'data = {8,7,6,5}' - >'data_trimmed = {7,6}' - >'MIN(data_trimmed)= 6 ')。但是,當數據輸入未被排序時,它會返回一個錯誤的MIN(例如'data = {6,8,5,7}' - >'data_trimmed = {8,5}' - >'MIN(data_trimmed)= 5')。因此,我想,數據需要在開始時進行排序(升序/降序)。 – StefanOverFlow