2016-11-27 150 views
0

我有兩列,數據爲雙列。 我想創建一個這樣做的vba腳本:VBA - 爲兩列中的每一列找到最大值和最小值

創建一個臨時的第三列,存儲每行的較低值。

輸出第三列的最小值和最大值。現在

問題不是創建這個第三列,我想在內存中完成此操作。

請幫 感謝

+0

顯示您嘗試代碼 – user3598756

+0

而且你問題標題(查找兩列中的每一列的最大值和最小值)與您的敘述不一致(每行下限值的輸出最小值和最大值):請澄清 – user3598756

+1

,如{= MIN(IF(A1:A6> B1:B6, B1:B6,A1:A6))&「 - 」&MAX(IF(A1:A6> B1:B6,B1:B6,A1:A6))''會做什麼? –

回答

0

這將顯示與最小和最大價值一個消息框,一個消息框,假設兩列A和B.

Sub MinMax() 

    ' Count the number of rows in the first column 
    ' Assuming that number of rows in second column equals those in the first column 
    HowFar = WorksheetFunction.CountA(Range("A:A")) 

    ' Declare an array to store the minimums 
    Dim Arr() As Double 
    ReDim Arr(HowFar - 2) 
    Dim Index As Integer 
    Index = 0 

    ' Loop through the first and second columns and store the minimums for each row in the array 
    Dim i As Integer 
    For i = 2 To HowFar 

     Minimum = Application.WorksheetFunction.Min(Range("A" & i & ":B" & i)) 

     Arr(Index) = Minimum 
     Index = Index + 1 

    Next i 

    ' Get the minimum value in the array 
    Min = Application.WorksheetFunction.Min(Arr) 

    ' Get the maximum value in the array 
    Max = Application.WorksheetFunction.Max(Arr) 

    ' MsgBox the two values 
    MsgBox ("Minimum = " & Min) 
    MsgBox ("Maximum = " & Max) 

End Sub 
相關問題