Consulting

Results 1 to 8 of 8

Thread: Solved: Filter unique values and count their frequency

  1. #1
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location

    Solved: Filter unique values and count their frequency

    Hi,

    I have a code which is intended to first filter out all unique values from a sheet and paste these values into sheet2.
    Then I want to determine the size of this range where the unique values where pasted. For every value within this range it should step through each value and count how many times each such value occurs on the original sheet (that is, the sheet from where the unique values were extracted from)

    Though I have not managed to resize the range, called malomrade, properly to make it only contain cells which contains a unique value, and hence make the For Each c mechanism step through only the cells which actually contains values and make a count for every such cell. Any ideas how this is done? I have below marked it something?? to indicate where I have a hard time coming up with how to VBA code it. Of course maybe it can also be done in an alternate way, all ideas are welcome.

    [VBA]Sub test()
    Dim malomrade As Range
    Dim c As Range

    Set malomrade = Sheets(2).Range("A1:A100")

    Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:= _
    malomrade, Unique:=True


    Set malomrade = malomrade.Resize(something??.Rows.Count)

    For Each c In malomrade

    c.Offset(0, 1) = WorksheetFunction.CountIf(Worksheets(1).Range("A:A"), c.Value)

    Next c

    End Sub[/VBA]


    I want it to look as it does in the attached file on sheet2. (Though there I have manually erased the values in column B for values from row 19 and below since it goes through down to row 100 because malomrade is as described above no resized properly).
    On sheet1 is the intitial values from where the unique values are extracted and from where the count should be made in the last step of the code.

  2. #2
    VBAX Tutor david000's Avatar
    Joined
    Mar 2007
    Location
    Chicago
    Posts
    276
    Location
    You could try a, Pivot Table, they automatically count text entries.
    I just copied the entire row and the pivot table wizard did the rest.

  3. #3
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    Hi,

    Yeah by using the data in column A as the element both for the row field and the data field it seems to display all unique values in the row field and count the number of occurrences of every such unique value in the data field. Thanks for your help.

    To improve my knowledge of the resize property, is there some way to determine which cells that actually contain a value and resize the range to include only cells meeting that criteria?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Rows.Count and Columns.Count of the 'something' give you that.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    Ok thanks, but what I have a hard time coding is how to find out the size of the range where all the unique values were pasted. This range will of course differ from time to time depending on how many unique values there are in the sheet at hand. For ex if there would be 25 unique values then I want to first find out how big the range should be, in this case 25 cells, and then do the resize as you suggested.
    So in short, some way to determine how big the range is at each current occassion and then do the resize to fit that occassion's number of unique values.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    first_cell_of_target.CurrentRegion.Rows.Count
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I put this together a while ago. Any use?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Regular
    Joined
    Aug 2006
    Posts
    58
    Location
    Thanks, adding your extra line of code did the trick. Great!

    [VBA]
    Sub test()
    Dim malomrade As Range
    Dim c As Range

    Set malomrade = Sheets(2).Range("A1:A100")

    Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:= _
    malomrade, Unique:=True


    Set malomrade = malomrade.Resize(Sheets(2).Range("A1").CurrentRegion.Rows.Count)

    For Each c In malomrade

    c.Offset(0, 1) = WorksheetFunction.CountIf(Worksheets(1).Range("A:A"), c.Value)

    Next c

    End Sub
    [/VBA]

    Mdmackillop, thanks for the page with more info you provided!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •