如何在Excel中动态标记列?

如何在Excel中动态标记列?,excel,vba,dynamic,excel-formula,spreadsheet,Excel,Vba,Dynamic,Excel Formula,Spreadsheet,我有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

我有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
集群1:资产<100000美元
集群2:100000=<资产<200000美元
集群3:资产>=300000
我想使用Excel,基本上做以下工作:

标记每个集群中帐户的某个阈值,该阈值将由用户输入

例如,用户说只有5%的集群2应该被标记,因此excel函数应该随机标记集群2中5%的帐户为“是”,以此类推。我希望它是交互式的,这就是为什么我希望用户在excel中的单元格中输入参数,这将随机更改集群中帐户旁边的标志值。 有什么方法可以在Excel中实现这一点吗?

A列:名称
B栏:资产
C列:集群
D列:临时(=排序)
E列:标志

复制一份你的工作簿。打开VBE(使用Alt F11)并插入模块(菜单->插入->模块)。在模块中插入下面的代码并执行程序“main”:在Sub main()中按F5

Sub-Main()
'将光标放在此处,然后按F5。
Application.ScreenUpdating=False
暗淡的细胞范围
设置ActCell=ActiveCell
呼叫总数
拨打随机号码
称之为巫术
呼叫设置标志
ActCell。选择
Application.ScreenUpdating=True
端接头
小计总数()
范围(“H8”)=“群集”
范围(“H9”)=1
范围(“H10”)=2
范围(“H11”)=3
范围(“I8”)=“标志%”
如果范围(“I9”)=那么范围(“I9”)=2%
如果范围(“I10”)=那么范围(“I10”)=5%
如果范围(“I11”)=那么范围(“I11”)=8%
范围(“J8”)=“计数”
范围(“J9:J11”)。公式1c1=“=Int(RC[-1]*RC[1])”
范围(“K8”)=“总计”
范围(“K9”)。公式=“=COUNTIF($C$2:$C$2001,“=1”)”
范围(“K10”)。公式=“=COUNTIF($C$2:$C$2001,”=2”)
范围(“K11”)。公式=“=COUNTIF($C$2:$C$2001,“=3”)”
端接头
子随机数()
Application.Calculation=xlManual
范围(“D2:D2001”)。公式=“=int(rand()*1e6)”
范围(“D2:D2001”)。副本
范围(“D2:D2001”)。粘贴特殊粘贴:=XLPasteValue
Application.CutCopyMode=False
端接头
Sub-SortRandomOLD()
ActiveWorkbook.Worksheets(“Tabelle2”).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(“Tabelle2”).Sort.SortFields.Add键:=范围(_
“C2:C2001”),SortOn:=xlSortOnValues,顺序:=xlAscending,数据选项:=_
xlSortNormal
ActiveWorkbook.Worksheets(“Tabelle2”).Sort.SortFields.Add键:=范围(_
“D2:D2001”),SortOn:=xlSortOnValues,顺序:=xlAscending,数据选项:=_
xlSortNormal
使用ActiveWorkbook.Worksheets(“Tabelle2”).Sort
.SetRange范围(“A1:E2001”)
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
申请
以
端接头
次排序法()
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add键:=范围(_
“C2:C2001”),SortOn:=xlSortOnValues,顺序:=xlAscending,数据选项:=_
xlSortNormal
ActiveSheet.Sort.SortFields.Add键:=范围(_
“D2:D2001”),SortOn:=xlSortOnValues,顺序:=xlAscending,数据选项:=_
xlSortNormal
使用ActiveSheet.Sort
.SetRange范围(“A1:E2001”)
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
申请
以
端接头
子集合标志()
Dim CLUSTER1总计为整数
Dim CLUSTER2总计为整数
Dim CLUSTER3总计为整数
Dim Cluster1Flag为整数
Dim Cluster2Flag为整数
Dim Cluster3Flag为整数
Cluster1Total=Application.WorksheetFunction.CountIf(范围(“C2:C2001”),“=1”)
Cluster2Total=Application.WorksheetFunction.CountIf(范围(“C2:C2001”),“=2”)
Cluster3Total=Application.WorksheetFunction.CountIf(范围(“C2:C2001”),“=3”)
'Debug.Print cluster1总计
Cluster1FlagCount=范围(“J9”)。值
Cluster2FlagCount=范围(“J10”).值
Cluster3FlagCount=范围(“J11”).值
范围(“A1”)。自动过滤器
ActiveSheet.Range(“$A$1:$E$2001”)。自动筛选字段:=3,标准1:=1

Range(“E2:E2001”).Formula=“=IF(COUNTIF($C$2:C2,“=1”)Rank、Random和Large与条件格式相结合可以做到这一点。@cyboashu哇,我真的需要对本机函数进行更多的创新:)@cyboashu我是excel的新手,如果可能的话,你能给我看看这个函数吗?请注意,
RANK
是不推荐使用的,只是为了与2007年及以前版本兼容才包括在内。另外,
RANDOM
会在每次修改工作表时重新计算,使工作表表现得相当怪异。我会使用一些VBA。使用VBA:应用过滤器r在你想要的集群上的范围内。然后在一个数组中捕获结果。对其应用
Randomise
以选择随机项。使用
Find
设置这些项上的标志。我将其放在VBA脚本中,我将从“开发”选项卡访问该脚本。你能详细说明如何执行该脚本吗?非常感谢:)这就是e主意,是的;-)我已经试着给你上面需要的所有信息!”ActiveWorkbook.Worksheets(“Tabelle2”).Sort.SortFields.Clear“它显示了这个超出范围的下标上的运行时错误。此外,它没有生成任何标志,我实际上想要一个问题中提到的单独的列,它在每次运行脚本时都会被标记。我已将“ActiveWorkbook.Worksheets(“Tabelle2”)…”替换为“ActiveSheet…”。(见上文子SortRandom和子SortRandomOLD)。在列标志中,标记的项目应为“1”,未标记的项目应为“0”。
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