A 列:名称
B 列:资产
C 列:簇
D 列:温度(=排序)
E 列:标志
复制您的工作簿。打开 VBE(使用 Alt F11)并插入一个模块(菜单 -> 插入 -> 模块)。在模块中插入以下代码并执行“main”过程:在 Sub main() 中按 F5
Sub Main()
'Put the cursor HERE and press F5.
Application.ScreenUpdating = False
Dim ActCell As Range
Set ActCell = ActiveCell
Call CountTotals
Call RandomNumber
Call SortRandom
Call SetFlag
ActCell.Select
Application.ScreenUpdating = True
End Sub
Sub CountTotals()
Range("H8") = "Cluster"
Range("H9") = 1
Range("H10") = 2
Range("H11") = 3
Range("I8") = "Flag%"
If Range("I9") = "" Then Range("I9") = "2%"
If Range("I10") = "" Then Range("I10") = "5%"
If Range("I11") = "" Then Range("I11") = "8%"
Range("J8") = "Count"
Range("J9:J11").FormulaR1C1 = "=Int(RC[-1]*RC[1])"
Range("K8") = "Total"
Range("K9").Formula = "=COUNTIF($C$2:$C$2001,""=1"")"
Range("K10").Formula = "=COUNTIF($C$2:$C$2001,""=2"")"
Range("K11").Formula = "=COUNTIF($C$2:$C$2001,""=3"")"
End Sub
Sub RandomNumber()
Application.Calculation = xlManual
Range("D2:D2001").Formula = "=int(rand()*1e6)"
Range("D2:D2001").Copy
Range("D2:D2001").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
End Sub
Sub SortRandomOLD()
ActiveWorkbook.Worksheets("Tabelle2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Tabelle2").Sort.SortFields.Add Key:=Range( _
"C2:C2001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("Tabelle2").Sort.SortFields.Add Key:=Range( _
"D2:D2001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Tabelle2").Sort
.SetRange Range("A1:E2001")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sub SortRandom()
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range( _
"C2:C2001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range( _
"D2:D2001"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveSheet.Sort
.SetRange Range("A1:E2001")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sub SetFlag()
Dim Cluster1Total As Integer
Dim Cluster2Total As Integer
Dim Cluster3Total As Integer
Dim Cluster1Flag As Integer
Dim Cluster2Flag As Integer
Dim Cluster3Flag As Integer
Cluster1Total = Application.WorksheetFunction.CountIf(Range("C2:C2001"), "=1")
Cluster2Total = Application.WorksheetFunction.CountIf(Range("C2:C2001"), "=2")
Cluster3Total = Application.WorksheetFunction.CountIf(Range("C2:C2001"), "=3")
'Debug.Print Cluster1Total
Cluster1FlagCount = Range("J9").Value
Cluster2FlagCount = Range("J10").Value
Cluster3FlagCount = Range("J11").Value
Range("A1").AutoFilter
ActiveSheet.Range("$A$1:$E$2001").AutoFilter Field:=3, Criteria1:="1"
Range("E2:E2001").Formula = "=IF(COUNTIF($C$2:C2,""=1"")<=" & Cluster1FlagCount & ",1,0)"
ActiveSheet.Range("$A$1:$E$2001").AutoFilter Field:=3, Criteria1:="2"
Range("E2:E2001").Formula = "=IF(COUNTIF($C$2:C2,""=1"")<=" & Cluster2FlagCount & ",1,0)"
ActiveSheet.Range("$A$1:$E$2001").AutoFilter Field:=3, Criteria1:="3"
Range("E2:E2001").Formula = "=IF(COUNTIF($C$2:C2,""=1"")<=" & Cluster3FlagCount & ",1,0)"
Range("A1").AutoFilter
End Sub