Dear expert!
I stoped on:
"Then I would go through a rolling group of three names
until I had the Minimum
Distance criterion satisfied; and delete the rest of the
names"
I need technical assistance again.
Best regards
Mark
Mark,
Try out the following SUB and see if it comes close to what you want.
The SUB assumes that your data is contiguous (no blank rows). It also assumes
that for every name, there is a series of three that will meet the Minimum
Distance Criteria. In other words, it does not handle the situation in which
the sum of the Distances for a Name is less than 500.
It also leaves intermediate worksheets in place, and does not give them any
particular name.
But it is a first stab.
=================================
Option Explicit
Sub Res()
Dim tbl As Range, c As Range
Dim Count As Integer
Const ResCount As Integer = 3
Const MinSumDistance As Integer = 500
Dim i As Long, j As Long
Dim SumDistance As Integer
Dim SumData As Double
Dim CurName As String
Dim Header() As String
Dim ColCt As Integer
Dim Grp(ResCount - 1, 2)
'copy data table to new sheet and sort
ActiveCell.CurrentRegion.Copy
Sheets.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks
_
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveCell.CurrentRegion.Sort Key1:=Range("A2"), Order1:=xlAscending,
Key2:= _
Range("B2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom,
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
Set tbl = ActiveCell.CurrentRegion
ColCt = tbl.Columns.Count
i = 2
Application.ScreenUpdating = False
Do
CurName = tbl.Cells(i, 1)
Do
SumDistance = 0
For j = 0 To 2
Grp(j, 0) = tbl.Cells(i, 1) 'Name
Grp(j, 1) = tbl.Cells(i, 2) 'Data
Grp(j, 2) = tbl.Cells(i, 3) 'Distance
i = i + 1
Next j
For j = 0 To 2
SumDistance = SumDistance + Grp(j, 2)
Next j
If SumDistance < MinSumDistance Then
tbl.Cells(i - 3, 1).EntireRow.Hidden = True
i = i - 2
End If
Loop Until SumDistance >= MinSumDistance
Do While tbl.Cells(i, 1) = CurName
tbl.Cells(i, 1).EntireRow.Hidden = True
i = i + 1
Loop
Loop Until tbl.Cells(i, 1) = ""
'Move processed cells to another worksheet
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
Sheets.Add
ActiveSheet.Paste
Application.CutCopyMode = False
'Set up report
'Get header line
ReDim Header(1 To ColCt)
For j = 1 To ColCt
Header(j) = Cells(1, j)
Next j
i = 1
Range(Cells(i, 1), Cells(i, ColCt)).Delete (xlShiftUp)
Do
Range(Cells(i, 1), Cells(i + 2, ColCt)).Insert (xlShiftDown)
CurName = Cells(i + 4, 1)
SumData = 0
SumDistance = 0
'insert header row
For j = 1 To ColCt
Cells(i + 2, j) = Header(j)
Next j
For j = 3 To 5
SumData = SumData + Cells(i + j, 2)
SumDistance = SumDistance + Cells(i + j, 3)
Next j
Cells(i + 1, 1) = "Min_result for " _
& CurName & " = " & Format(SumData, "#.00") & _
", Distance = " & SumDistance
i = i + 6
Loop Until Cells(i, 1) = ""
Application.ScreenUpdating = True
[A1].Select
End Sub
===========================
--ron