简体   繁体   中英

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". 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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