我只需要对九个文本框值中的五个最大数字进行求和
TextBox11 = WorksheetFunction.Sum(Large((Val(TextBox2.Text) + Val(TextBox3.Text) + Val(TextBox4.Text) + Val(TextBox5.Text) + Val(TextBox6.Text) + Val(TextBox7.Text) + _
Val(TextBox8.Text) + Val(TextBox9.Text) + Val(TextBox10.Text)), " 1, 2, 3, 4, 5"))
仅对九个数字中的五个大数进行求和
35
(9+8+7+6+5
)。Sub SumUpTopValues()
Const TOP_VALUES As Long = 5
' Simple Test
Dim Values() As Variant: Values = Array(3, 2, 1, 5, 7, 6, 4, 8, 9)
With Application
MsgBox .Sum(.Large(Values, _
Application.Evaluate("ROW(1:" & TOP_VALUES & ")")))
End With
' For Real
' Dim Values() As Variant: Values = Array( _
' Val(TextBox2.Text), Val(TextBox3.Text), Val(TextBox4.Text), _
' Val(TextBox5.Text), Val(TextBox6.Text), Val(TextBox7.Text), _
' Val(TextBox8.Text) + Val(TextBox9.Text) + Val(TextBox10.Text))
'
' With Application
' TextBox11.Text = .Sum(.Large(Values, _
' Application.Evaluate("ROW(1:" & TOP_VALUES & ")")))
' End With
End Sub