Both of those are easy to do with a formula. For example:
Book1 |
---|
|
---|
| A | B | C | D | E |
---|
1 | Value | Condition | | # Unique | # Unique w/1 |
---|
2 | A | 1 | | 5 | 3 |
---|
3 | B | 2 | | | |
---|
4 | C | 1 | | | |
---|
5 | D | 2 | | | |
---|
6 | E | 1 | | | |
---|
7 | A | 1 | | | |
---|
8 | B | 2 | | | |
---|
9 | C | 1 | | | |
---|
10 | D | 2 | | | |
---|
11 | E | 1 | | | |
---|
12 | A | 1 | | | |
---|
13 | B | 2 | | | |
---|
14 | C | 1 | | | |
---|
15 | D | 2 | | | |
---|
16 | E | 1 | | | |
---|
|
---|
So you could write this in a VBA program using the Evaluate function:
VBA Code:
MyCount = EVALUATE("=ROWS(UNIQUE(A2:A16))")
The "traditional" way of writing it would be to find the last row, set up a loop and check each row. Something like:
VBA Code:
Sub Test1()
Dim lr As Long, r As Long, MyDic As Object, MyDic2 As Object
Set MyDic = CreateObject("Scripting.Dictionary")
Set MyDic2 = CreateObject("Scripting.Dictionary")
lr = Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To lr
MyDic(Cells(r, "A")) = MyDic(Cells(r, "A")) + 1
If Cells(r, "B") = 2 Then MyDic2(Cells(r, "B")) = MyDic2(Cells(r, "B")) + 1
Next r
Debug.Print MyDic.Count, MyDic2.Count
End Sub
But that's likely to be less efficient than the EVALUATE, which is less efficient than the sheet formula. I assume you're looking for techniques, not a specific macro here. If so, what exactly is your final goal?