gpt4 book ai didi

arrays - 在数组 Excel VBA 中返回最小值的索引

转载 作者:行者123 更新时间:2023-12-04 20:05:55 37 4
gpt4 key购买 nike

我试图从 sum(3) 数组中找到最小值的索引。它应该被分配给一个变量 min

min = 最小值的索引

我应该先对数组进行排序还是有任何直接的方法?
这是我的代码:

 `Sub Min_index()
Dim Pt_array(3) As Single
Pt_array(0) = 0
Pt_array(1) = 12.3
Pt_array(2) = 16.06
Pt_array(3) = 20.11

Dim Ad_E_array(3) As Single
Dim Lo_E_array(3) As Single
Dim Bs_temp As Single

Dim i As Integer
i = 0
Do While i < 4
Bs_temp = BS
Ad_E_array(i) = Ad_E 'defined in previous Sub
Lo_E_array(i) = Lo_E 'defined in previous Sub

If Bs_temp + Pt_array(i) - Qth < BS_Maximum_limit Then
Bs_temp = Bs_temp + Pt_array(i) - Qth
Ad_E_array(i) = Ad_E_array(i) + 0
Lo_E_array(i) = Lo_E_array(i) + 0
Call function_decide(int_forecast_hour - 1, Bs_temp, Qth + 1, Lo_E_array(i), Ad_E_array(i))
Else
Lo_E_array(i) = Pt_array(i) - Qth - (BS_Maximum_limit - Bs_temp)
Bs_temp = BS_Maximum_limit
Call function_decide(int_forecast_hour - 1, Bs_temp, Qth + 1, Lo_E_array(i), Ad_E_array(i))
End If
i = i + 1
Loop

Dim sum(3) As Single
Dim min As Single
i = 0
Do While i < 4
sum(i) = Abs(Lo_E_array(i)) + Abs(Ad_E_array(i))
i = i + 1
Loop
End Sub`

最佳答案

您可以通过 Excel Application object 使用工作表的 MIN functionMATCH function 来接收包含最小值的元素的从 1 开始的索引位置。

Sub wqewuiew()
Dim Pt_array(3) As Single, p As Long
Pt_array(0) = 1000
Pt_array(1) = 12.3
Pt_array(2) = 16.06
Pt_array(3) = 20.11

p = Application.Match(Application.Min(Pt_array), Pt_array, 0)

Debug.Print p '<~~ 'p' is 2 (I changed the value of the first array element)
End Sub

关于arrays - 在数组 Excel VBA 中返回最小值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39652293/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com