2016-11-27 154 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 
相关问题