The work on the leaflet is slow, so the forming of the matrix and the billing of the mass is used only to unload the result.Definition Max♪ Plus and their position in the matrix occurs in one pair of cycles. This reduced the time of implementation, but slightly increased the memory used (two variables added). Cycle feedback. If they found the value Plus, we'll cut the external check. But there's a moment... Maybe some of the same. Max and in cycles with reverse counting of buoys, a crossing for the upper left Max♪To avoid error, summation Max and Plus dispersed separately, since in theory there may be no positive value.Dimensions of line/bottle values Max or Plus:Private Sub cmdb2_Click()
Dim arrM()
Dim lRw As Long, lClmn As Long, lMax As Long
Dim i As Long, j As Long, lSum As Long
Dim iRowMax As Long, jColMax As Long, iRowPlus As Long, jColPlus As Long
Cells.Clear
lRw = Val(txt1.Value): lClmn = Val(txt2.Value)
If lRw <= 0 Or lClmn <= 0 Then Exit Sub
ReDim arrM(1 To lRw, 1 To lClmn + 5)
For i = lRw To 1 Step -1
For j = lClmn To 1 Step -1
arrM(i, j) = Int(-19 * Rnd + 10)
If arrM(i, j) >= lMax Then
lMax = arrM(i, j): iRowMax = i: jColMax = j
End If
If iRowPlus = 0 Then
If arrM(i, j) > 0 Then iRowPlus = i: jColPlus = j
End If
Next j
Next i
For i = 1 To lRw
lSum = lSum + arrM(i, jColMax)
Next i
For j = 1 To lClmn
lSum = lSum + arrM(iRowMax, j)
Next j
arrM(1, lClmn + 2) = "Max элемент"
arrM(2, lClmn + 2) = lMax
arrM(1, lClmn + 3) = "Сумма эл-ов с Max"
arrM(2, lClmn + 3) = lSum - lMax
lSum = 0
If iRowPlus > 0 Then
For i = 1 To lRw
lSum = lSum + arrM(i, jColPlus)
Next i
For j = 1 To lClmn
lSum = lSum + arrM(iRowPlus, j)
Next j
End If
arrM(1, lClmn + 4) = "Plus элемент"
arrM(2, lClmn + 4) = arrM(iRowPlus, jColPlus)
arrM(1, lClmn + 5) = "Сумма эл-ов с Plus"
arrM(2, lClmn + 5) = lSum - arrM(iRowPlus, jColPlus)
Cells(1, 1).Resize(lRw, UBound(arrM, 2)).Value = arrM
End Sub
Second option is the sum at the crossing of the extreme right Maxrange to extreme right Plus (including):Private Sub cmdb2_Click()
Dim arrM()
Dim lRw As Long, lClmn As Long, lMax As Long
Dim i As Long, j As Long, lSum As Long, a As Long, b As Long
Dim iRowMax As Long, jColMax As Long, iRowPlus As Long, jColPlus As Long
Cells.Clear
lRw = Val(txt1.Value) + 1: lClmn = Val(txt2.Value)
a = Val(txt3.Value): b = Val(txt4.Value)
If lRw <= 0 Or lClmn <= 0 Then Exit Sub
lMax = a - 1
ReDim arrM(1 To lRw, 1 To lClmn + 5)
arrM(1, 1) = "Матрица " & lRw - 1 & " x " & lClmn
For i = 2 To lRw
For j = 1 To lClmn
arrM(i, j) = Int((b - a + 1) * Rnd + a)
If arrM(i, j) >= lMax Then
lMax = arrM(i, j): iRowMax = i: jColMax = j
End If
If arrM(i, j) > 0 Then
iRowPlus = i: jColPlus = j
End If
Next j
Next i
For i = 2 To lRw
lSum = lSum + arrM(i, jColMax)
Next i
For j = 1 To lClmn
lSum = lSum + arrM(iRowMax, j)
Next j
arrM(1, lClmn + 2) = "Max элемент"
arrM(1, lClmn + 3) = "Сумма эл-ов с Max"
arrM(2, lClmn + 2) = lMax
arrM(2, lClmn + 3) = lSum - lMax
arrM(1, lClmn + 4) = "Plus элемент"
arrM(1, lClmn + 5) = "Сумма эл-ов с Plus"
If iRowPlus > 0 Then
lSum = 0
For i = 2 To iRowPlus
For j = 1 To jColPlus
lSum = lSum + arrM(i, j)
Next j
Next i
arrM(2, lClmn + 4) = arrM(iRowPlus, jColPlus)
arrM(2, lClmn + 5) = lSum
End If
Cells(1, 1).Resize(UBound(arrM), UBound(arrM, 2)).Value = arrM
End Sub