3

How to find smallest element of array V(12,9) and its number?

Private Sub Command2_Click()
Dim V(1 To 12, 1 To 9) As Integer
Randomize
For i = 1 To 12
For j = 1 To 9
V(i, j) = Rnd * 50
Next j
Next i
xktg
  • 41
  • 5

2 Answers2

2

Identify the Minimum Value in a 2D Array

  • See the information and results in the Immediate window (Ctrl+G). It's nicer and more educational than the presentation in the message box.
  • With such small numbers you could replace all the Longs with Integers if that is a requirement. Here is a link describing why we mostly don't use Integer anymore.
Private Sub Command2_Click()
    
    Const Max As Long = 50
    
    ' Populate the array.

    Dim V(1 To 12, 1 To 9) As Long
    
    Dim i As Long
    Dim j As Long
    
    Randomize
    For i = 1 To 12
        For j = 1 To 9
            V(i, j) = Rnd * Max
        Next j
    Next i
    
    Debug.Print GetDataString(V, , , "Random numbers from 0 to " & Max)
    
    Debug.Print "How Min Was Changed in the Loop (It Started at " & Max & ")"
    Debug.Print "The array was looped by rows."
    Debug.Print "Visually find the following values to understand what happened."
    Debug.Print "i", "j", "Min"

    ' Calculate the minimum.
    
    Dim Min As Long: Min = Max
    
    For i = 1 To 12
        For j = 1 To 9
            If V(i, j) < Min Then
                Min = V(i, j)
                Debug.Print i, j, Min
            End If
        Next j
    Next i
    
    Debug.Print "The minimum is " & Min & "."
    
    MsgBox GetDataString(V, , , "Random numbers from 0 to " & Max) & vbLf _
        & "The minimum is " & Min & ".", vbInformation
    
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Purpose:      Returns the values of a 2D array in a string.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetDataString( _
    ByVal Data As Variant, _
    Optional ByVal RowDelimiter As String = vbLf, _
    Optional ByVal ColumnDelimiter As String = " ", _
    Optional ByVal Title As String = "PrintData Result") _
As String
    
    ' Store the limits in variables
    Dim rLo As Long: rLo = LBound(Data, 1)
    Dim rHi As Long: rHi = UBound(Data, 1)
    Dim cLo As Long: cLo = LBound(Data, 2)
    Dim cHi As Long: cHi = UBound(Data, 2)
    
    ' Define the arrays.
    Dim cLens() As Long: ReDim cLens(rLo To rHi)
    Dim strData() As String: ReDim strData(rLo To rHi, cLo To cHi)
    
    ' For each column ('c'), store strings of the same length ('cLen')
    ' in the string array ('strData').
    
    Dim r As Long, c As Long
    Dim cLen As Long
    
    For c = cLo To cHi
        ' Calculate the current column's maximum length ('cLen').
        cLen = 0
        For r = rLo To rHi
            strData(r, c) = CStr(Data(r, c))
            cLens(r) = Len(strData(r, c))
            If cLens(r) > cLen Then cLen = cLens(r)
        Next r
        ' Store strings of the same length in the current column
        ' of the string array.
        If c = cHi Then ' last row (no column delimiter ('ColumnDelimiter'))
            For r = rLo To rHi
                strData(r, c) = Space(cLen - cLens(r)) & strData(r, c)
            Next r
        Else ' all but the last row
            For r = rLo To rHi
                strData(r, c) = Space(cLen - cLens(r)) & strData(r, c) _
                    & ColumnDelimiter
            Next r
        End If
    Next c
    
    ' Write the title to the print string ('PrintString').
    Dim PrintString As String: PrintString = Title
    
    ' Append the data from the string array to the print string.
    For r = rLo To rHi
        PrintString = PrintString & RowDelimiter
        For c = cLo To cHi
            PrintString = PrintString & strData(r, c)
        Next c
    Next r
    
    ' Assign print string as the result.
    GetDataString = PrintString

End Function
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
0

First you need to declare the data type of variables i and j

Dim i as Long
Dim j as Long

second, your array name V not A so correct this line

V(i, j) = Rnd * 50

finally, if your array contains numbers you can use this line

Debug.Print WorksheetFunction.Min(V)
cooogeee
  • 181
  • 1
  • 10
  • 1
    Dim i,j as Integer means that i was declared as a variant. In VBA You must specify the type of each variable. You cannot specify the type of a group of variables – freeflow Nov 19 '22 at 14:33
  • i and j are the dimension of the array – cooogeee Nov 19 '22 at 14:38
  • 3
    You have effectively declared I as a variant and j as an integer. You should also be aware that integer types cannot be greater than 32,767 which is less than the number of rows in Excel. It may be better to declare them as longs. – Ron Rosenfeld Nov 19 '22 at 15:00