Dim ss As List(of String) = DataTables("表A").GetValues("项目", Tables("表A").Filter)
For Each s As String In ss
Dim drs As List(of DataRow) = DataTables("表A").Select("项目='" & s & "' and " & Tables("表A").Filter, "单位")
drs(0)("预赛组别") = 1
For i As Integer = 1 To drs.Count - 1
Dim i1 As Integer = drs(i-1)("预赛组别")
If i1 >= Math.Ceiling(drs.Count/8)
For i2 As Integer = 1 To 8
Dim i3 As Integer = DataTables("表A").Compute("Count(项目)","项目='" & s & "' and 预赛组别='" & i2 & "' and " & Tables("表A").Filter)
If i3 < Math.Ceiling(drs.Count/8)
drs(i)("预赛组别") = 1
Else
drs(i)("预赛组别") = i3
End If
Next
Else
drs(i)("预赛组别") = i1 + 1
End If
Next
Next
For Each s As String In ss
Dim ss1 As List(of String) = DataTables("表A").GetValues("预赛组别","项目='" & s & "' and " & Tables("表A").Filter)
For Each s1 As Integer In ss1
' Dim drs As List(of DataRow) = DataTables("表A").Select("项目='" & s & "'and 预赛组别=" & s1,"单位")
Dim drs As List(of DataRow) = DataTables("表A").Select("项目='" & s & "'and 预赛组别='" & s1 & "' and " & Tables("表A").Filter,"单位")
drs(0)("预赛道次") = 1
For i As Integer = 1 To drs.Count - 1
drs(i)("预赛道次") = drs(i-1)("预赛道次") + 1
Next
For i As Integer = 1 To drs.Count - 1
If drs(i)("单位") = drs(i-1)("单位")
If (i-4)>=0
drs(i)("预赛道次") = i-4+1
drs(i-4)("预赛道次") = i+1
ElseIf (i+4)<=drs.Count
drs(i)("预赛道次") = i+4+1
drs(i+4)("预赛道次") = i+1
End If
End If
Next
Next
Next
Tables("表A").Sort = "项目,预赛组别,预赛道次"