我有excel数据,例如:
Name Assets Cluster Flag
ABN AMRO $100,000 2
Allahabad Bank $200,000 2
Allen & Company $50,000 1
Bank of America $200,000 3
Barclays Capital $300,000 3
BB&T $70,000 1
BBY Ltd. $150,000 2
Berkery, Noyes & Co. $200,000 3
BG Capital $90,000 1
Blackstone $400,000 3
以此类推,大约有2000条记录。
现在我将它们分为3个类:
Cluster 1: Assets < $100,000
Cluster 2: 100,000 =< Assets < $200,000
Cluster 3: Assets >= 300,000
我想使用Excel来基本上执行以下操作:
在每个群集中标记一定的帐户阈值,该阈值将由用户输入。
例如,用户说应仅标记群集2中5%的帐户,因此excel函数应将“是”随机标记为群集2中5%的帐户,依此类推。我希望它是交互式的,这就是为什么我希望用户在excel的单元格中输入参数,这会随机更改群集中帐户旁边的标志值。 有什么办法可以在Excel中实现?
答案 0 :(得分:1)
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