简体   繁体   English

VBA从列表中提取唯一值并在单独的页面上计算它们

[英]VBA to extract unique values from list and count them on separate page

Ok, this has to be the easiest question ever, but I've been searching for awhile and can't figure this out. 好吧,这一直是最简单的问题,但我一直在寻找一段时间而无法解决这个问题。 I have a very long list of values on sheet "Page1_1" (shtData) which I need to use to generate a list of unique values, along with a count of each unique value, on "Numbers". 我在工作表“Page1_1”(shtData)上有一个非常长的值列表,我需要使用它来生成“Numbers”上的唯一值列表以及每个唯一值的计数。 It's very simple to do manually by just copying to "Numbers" (shtNumbers) and removing duplicates, but the countif formula I use is very memory intensive, and bogs it all down. 通过复制到“Numbers”(shtNumbers)并删除重复项来手动完成非常简单,但我使用的countif公式非常耗费内存,并且将其全部搞砸了。 I have code to provide the unique list, but trying to get the count of each item is stumping me 我有代码提供唯一的列表,但试图得到每个项目的计数是困扰我

Put simply: I need code to generate a list of unique values from "Page1_1" (shtData) A2:end of data, and put that list starting on "Numbers" (shtNumbers) A2 and down, then count occurrences and list the count in B2 and down. 简单地说:我需要代码从“Page1_1”(shtData)A2生成一个唯一值列表:数据结束,并将该列表从“Numbers”(shtNumbers)A2开始放下,然后计算出现次数并列出计数B2下来。

current code: 当前代码:

shtData.Range("A2:A65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=shtNumbers.Range("A2"), Unique:=True

That code generates the list for me perfectly. 该代码完美地为我生成了列表。 I'd post what I've got for the count part, but it doesn't work at all, so I doubt it'd be helpful at all. 我发布了我的计数部分,但它根本不起作用,所以我怀疑它会有所帮助。

Thanks! 谢谢!

Try this: 尝试这个:

With shtNumbers

    With .Range(.Range("B2"),.Range("A2").End(xlDown).Offset(,1))

        .FormulaR1C1 = "=COUNTIF(Data!C[-1],RC[-1])" 'change data to actual sheet name

    End With

End With

Give this a try: 尝试一下:

Sub SuperSimpleFrequencyTable()
    Dim C As Range, A As Range, B As Range

    Set A = Sheets("Page1_1").Range("A:A")
    Set B = Sheets("Numbers").Range("A:A")

    A.Copy B
    B.RemoveDuplicates Columns:=1, Header:=xlNo
    Set C = B.SpecialCells(xlCellTypeConstants).Offset(0, 1)

    With C
        .Formula = "=countif(Page1_1!A:A,A1)"
        .Value = .Value
    End With

End Sub

It will not leave formulas in the Numbers sheet. 它不会在Numbers表中留下公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM