Option Explicit
Private Const MIN_COLUMN_COUNT = 4
Sub Наибольшее_наименьшее()
CloseEmptyWb
JobRange sourceRange:=Range("B2:D16"), targetRange:=Workbooks.Add(1).Sheets(1).Range("B2")
End Sub
Private Sub JobRange(sourceRange As Range, targetRange As Range)
Application.ScreenUpdating = False
Dim Application_Calculation As XlCalculation: Application_Calculation = Application.Calculation: Application.Calculation = xlCalculationManual
Set targetRange = targetRange.Resize(sourceRange.Rows.Count, sourceRange.Columns.Count)
sourceRange.Copy targetRange
Dim srr As Variant, MinMax As Variant
srr = sourceRange.Value
Do
For Each MinMax In Array("max", "min")
If UBound(srr, 1) - CountEmptyRows(srr) = MIN_COLUMN_COUNT Then Exit Do
If CountEmptyRows(srr) = UBound(srr, 1) Then Exit Do
srr = ExceptMinMax(srr, MinMax)
Set targetRange = targetRange.Offset(0, targetRange.Columns.Count + 2)
sourceRange.Copy targetRange
targetRange.Value = srr
Next
DoEvents
Loop
Application.Calculation = Application_Calculation
Application.ScreenUpdating = True
End Sub
Private Function ExceptMinMax(ByVal srr As Variant, ByVal oper As String) As Variant
Dim ys As Long, xs As Long, maxVal As Long, maxRow As Long
Dim yrr As Variant
ReDim yrr(1 To UBound(srr, 1)) As Long
If oper = "max" Then
oper = "<"
Else
oper = ">"
End If
Do
If CountEmptyRows(srr) = UBound(srr, 1) Then Exit Do
If UBound(srr, 1) - CountEmptyRows(srr) <= MIN_COLUMN_COUNT Then Exit Do
For xs = 1 To UBound(srr, 2)
If myCountIf(yrr) >= UBound(srr, 2) Then Exit Do
For maxRow = 1 To UBound(srr, 1)
If Not IsEmpty(srr(maxRow, xs)) Then Exit For
Next
If maxRow <= UBound(srr, 1) Then
maxVal = srr(maxRow, xs)
For ys = maxRow + 1 To UBound(srr, 1)
If Compare(maxVal, oper, srr(ys, xs)) Then
maxVal = srr(ys, xs)
maxRow = ys
End If
Next
yrr(maxRow) = yrr(maxRow) + 1
End If
Next
For ys = 1 To UBound(srr, 1)
If yrr(ys) > 0 Then
If UBound(srr, 1) - CountEmptyRows(srr) <= MIN_COLUMN_COUNT Then Exit Do
For xs = 1 To UBound(srr, 2)
srr(ys, xs) = Empty
Next
End If
Next
DoEvents
Loop
ExceptMinMax = srr
End Function
Private Function Compare(maxVal As Long, oper As String, curVal As Variant) As Boolean
If Not IsEmpty(curVal) Then
If oper = "<" Then
Compare = maxVal < curVal
Else
Compare = maxVal > curVal
End If
End If
End Function
Private Function CountEmptyRows(srr As Variant) As Long
Dim ys As Long, xs As Long, flagEmpty As Boolean
For ys = 1 To UBound(srr, 1)
For xs = 1 To UBound(srr, 2)
If Not IsEmpty(srr(ys, xs)) Then GoTo nextRow
Next
CountEmptyRows = CountEmptyRows + 1
nextRow:
Next
End Function
Private Function myCountIf(yrr As Variant) As Long
Dim yy As Long
For yy = LBound(yrr) To UBound(yrr)
If yrr(yy) > 0 Then
myCountIf = myCountIf + 1
End If
Next
End Function
Private Sub CloseEmptyWb()
Dim wb As Workbook
For Each wb In Application.Workbooks
If wb.Path = "" Then wb.Close False
Next
End Sub
|