【问题标题】:VBA Get Max of First Dimension of ArrayVBA获取数组第一维的最大值
【发布时间】:2022-01-01 22:17:44
【问题描述】:

我只需要获取数组第一个维度的最大值。

WorksheetFunction.Max(shifts_array) 返回所有维度的最大值,但我只需要维度 1

数组是(1 to 10, 1 to 2)

【问题讨论】:

  • 该二维数组中的每个值都“在”两个维度中,所以您真的是指第一个“列”吗?您可以遍历感兴趣的元素,或使用 Application.Index 来“切片”数组 - stackoverflow.com/questions/7031416/…
  • 如何将数组值分配给工作表上的Range("A1:B10") 并获取MAX("A1:A10")?避免循环。

标签: excel vba


【解决方案1】:

二维数组的最大列数

Option Explicit

Sub SliceColumn()
    
    Const ColumnIndex As Long = 1 ' Modify this.
    
    Dim Data As Variant: Data = GetSampleData
    
    ' Using 'Application.Index'...
    Dim Data1 As Variant: Data1 = Application.Index(Data, 0, ColumnIndex)
    'Data1(1, 1) = CVErr(2007)
    
    ' ... or using 'GetColumn':
    'Dim Data1 As Variant: Data1 = GetColumn(Data, ColumnIndex)
    
    Dim MaxValue As Variant: MaxValue = Application.Max(Data1)
    If IsNumeric(MaxValue) Then
        Debug.Print "The maximum value is " & MaxValue
    End If

    Dim r As Long
    
    Debug.Print "Source"
    For r = 1 To UBound(Data, 1)
        Debug.Print Data(r, 1), Data(r, 2)
    Next r
    
    Debug.Print "Sliced Column " & ColumnIndex
    For r = 1 To UBound(Data1, 1)
        Debug.Print Data1(r, 1)
    Next r

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values from a column of a 2D array
'               in a 2D one-column array.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetColumn( _
    ByVal Data As Variant, _
    ByVal ColumnIndex As Long) _
As Variant
    Const ProcName As String = "GetColumn"
    On Error GoTo ClearError

    Dim LB1 As Long: LB1 = LBound(Data, 1)
    Dim UB1 As Long: UB1 = UBound(Data, 1)
    Dim LB2 As Long: LB2 = LBound(Data, 2)
    
    Dim cData As Variant: ReDim cData(LB1 To UB1, LB2 To LB2)
    
    Dim r As Long
    
    For r = LB1 To UB1
        cData(r, 1) = Data(r, ColumnIndex)
    Next r
    
    GetColumn = cData

ProcExit:
    Exit Function
ClearError:
    Debug.Print "'" & ProcName & "' Run-time error '" _
        & Err.Number & "':" & vbLf & "    " & Err.Description
    Resume ProcExit
End Function

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns a 2D one-based array containing sequential numbers
'               added by columns (one column at the time).
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetSampleData()
    Dim Data As Variant: ReDim Data(1 To 10, 1 To 2) ' e.g. increase 2
    Dim r As Long, c As Long, n As Long
    For c = 1 To UBound(Data, 2)
        For r = 1 To UBound(Data, 1)
            n = n + 1
            Data(r, c) = n
        Next r
    Next c
    GetSampleData = Data
End Function

【讨论】:

    【解决方案2】:

    如何将数组大小调整为 1 列

    ReDim Preserve MyArray(1 To UBound(MyArray), 1 To 1)
    MaxFirstColumn = WorksheetFunction.Max(MyArray)
    

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 1970-01-01
      • 2017-11-03
      • 2015-04-22
      • 2012-10-29
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-16
      相关资源
      最近更新 更多