-1

I have a VBA collection that contains several File items (I created a class module called File with several properties). I was curious if there is a way to use a For loop to iterate through all the items in the collection and do two things:

  1. Get a list of all unique items based on a File property that I specify
  2. Count the number of items that match each unique value from the above list

For example, let's say I have 4 File items in my collection:

  • File1 (File1.Pattern = "Test*.xlsx")
  • File2 (File2.Pattern = "Test*.xlsx")
  • File3 (File3.Pattern = "Test*.txt")
  • File4 (File4.Pattern = "Test*.csv")

The output I would want would be (ideally an array or separate collection):

  • Test*.xlsx (2)
  • Test*.txt (1)
  • Test*.csv (1)

Any thoughts on how I might implement something like this?

Community
  • 1
  • 1
Eli Greenberg
  • 311
  • 1
  • 7
  • 16

1 Answers1

3

As long as you don't need to do this on a Mac:

Dim d, f, k
Set d = CreateObject("scripting.dictionary")

For Each f in myFiles
    d(f.Pattern) = d(f.Pattern)+1
Next f

For Each k in d
    Debug.Print k, d(k) 
Next k
Tim Williams
  • 154,628
  • 8
  • 97
  • 125