2016-09-23 128 views
0

我試圖從sum(3)數組中找到最小值的索引。和它應該分配給一個變量分鐘最小值在數組Excel中返回最小值的索引VBA

分鐘= 索引

我應該陣列第一排序或有沒有這樣做它的任何直接的方法?
這裏是我的代碼:

`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` 

回答

1

您可以收到包含的最小值與一個Excel Application object的使用的元素的1開始的索引位置工作表的MIN functionMATCH function

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 
+0

謝謝@Jeeped 它的工作原理。 我也可以得到索引2的值(12.3)嗎? – Yousaf

+0

只是'Application.Min(Pt_array)'或更詳細的'pt_array(Application.Match(Application.Min(Pt_array),Pt_array,0)-1)'。 – Jeeped

+0

完美。謝謝 – Yousaf