simplify a formula

excelNewbie22

Well-known Member
Joined
Aug 4, 2021
Messages
534
Office Version
  1. 365
Platform
  1. Windows
hi !
i'm using this formula in g2,
Excel Formula:
=SUM(--(COUNTIF(A3:F3,A2:F2)>0))
but when i applied it on a very large quantity of rows it gets slow, so i thought and manage to use a simpler formula like countif, in a longer (text wise) but much faster formula like in h2,
and now i want to do the same but with COUNTUNIQUE UDF, but it's more complicated, and i thought maybe some of you can help simplify it? since my above method wouldn't work on countunique

test-.xlsm
ABCDEFGH
1smart waysimplify
212345633
3456789
412232
52385
test
Cell Formulas
RangeFormula
G2G2=SUM(--(COUNTIF(A3:F3,A2:F2)>0))
H2H2=COUNTIF(A2:F2,A3)+COUNTIF(A2:F2,B3)+COUNTIF(A2:F2,C3)+COUNTIF(A2:F2,D3)+COUNTIF(A2:F2,E3)+COUNTIF(A2:F2,F3)
G4G4=CountUnique(A4:D4,A5:D5)


VBA Code:
Function CountUnique(r1 As Range, r2 As Range)
  Dim i As Long, j As Long
  Dim a As Variant, b As Variant
  a = r1.Value
  b = r2.Value
 
  For i = 1 To UBound(a, 2)
    For j = 1 To UBound(b, 2)
      If b(1, j) = a(1, i) Then
        CountUnique = CountUnique + 1
        b(1, j) = "x"
        Exit For
      End If
    Next
  Next
End Function
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi @excelNewbie22 , I hope you are well.

Try the following macro instead of formulas or UDFs.
The macro will perform the count for all rows.

VBA Code:
Sub count_uniques()
  Dim a As Variant, b As Variant
  Dim i As Long, n As Long, j As Long, jj As Long
 
  a = Range("A2:F" & Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a, 1) Step 2
    n = 0
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        For jj = 1 To UBound(a, 2)
          If a(i + 1, jj) = a(i, j) Then
            n = n + 1
          End If
        Next
        b(i, 1) = n
      End If
    Next
  Next
  Range("G2").Resize(UBound(b)).Value = b
End Sub

------------------
But if you want the UDF, here it is updated:

VBA Code:
Function CountUnique(r1 As Range, r2 As Range)
  Dim j As Long, jj As Long, n As Long
  Dim a As Variant, b As Variant
  a = r1.Value
  b = r2.Value
 
  For j = 1 To UBound(b, 2)
    If a(1, j) <> "" Then
      For jj = 1 To UBound(b, 2)
        If a(1, j) = b(1, jj) Then
          n = n + 1
        End If
      Next
    End If
  Next
  CountUnique = n
End Function




--------------
Try it and tell us if it is faster and you no longer have slow problems in your sheet.
Cordially
Dante Amor
--------------​
 
Last edited:
Upvote 0
I corrected a small detail in the UDF, here is the updated code:

VBA Code:
Function CountUnique(r1 As Range, r2 As Range)
  Dim j As Long, jj As Long, n As Long
  Dim a As Variant, b As Variant
  a = r1.Value
  b = r2.Value
 
  For j = 1 To UBound(a, 2)
    If a(1, j) <> "" Then
      For jj = 1 To UBound(b, 2)
        If a(1, j) = b(1, jj) Then
          n = n + 1
        End If
      Next
    End If
  Next
  CountUnique = n
End Function

Dante Amor
ABCDEFG
1UDF
21234563
3456789
412233
52385
61234562
751
81234560
989
101234566
11123456
Hoja2
Cell Formulas
RangeFormula
G2,G10,G8,G6,G4G2=CountUnique(A2:F2,A3:F3)


:cool:
 
Upvote 0
hi dante, thanks for helping,
don't get me wrong, the first countunique i posted is fast, only slow down when fill down to a million (example) rows,
the udf you posted is wrong, see example
and while i'm appreciated your help, i prefer something more like in h2, if possible but for rows with duplicates like in the example for countunique (the original)

test.xlsb
ABCDEFGH
1UDFsimplify
212345633
3456789
412233should be 2
52385
61234562
751
81234560
989
101234566
11123456
1212236should be 4
131223
14
test
Cell Formulas
RangeFormula
G2,G12,G10,G8,G6,G4G2=CountUnique(A2:F2,A3:F3)
H2H2=COUNTIF(A2:F2,A3)+COUNTIF(A2:F2,B3)+COUNTIF(A2:F2,C3)+COUNTIF(A2:F2,D3)+COUNTIF(A2:F2,E3)+COUNTIF(A2:F2,F3)
 
Upvote 0
Here the corrected UDF:

VBA Code:
Function CountUnique(r1 As Range, r2 As Range)
  Dim j As Long, jj As Long, n As Long
  Dim a As Variant, b As Variant
  Dim dic As Object
  a = r1.Value
  b = r2.Value
  Set dic = CreateObject("Scripting.Dictionary")

  For j = 1 To UBound(a, 2)
    If a(1, j) <> "" Then
      If Not dic.exists(a(1, j)) Then
        dic(a(1, j)) = Empty
        For jj = 1 To UBound(b, 2)
          If a(1, j) = b(1, jj) Then
            n = n + 1
          End If
        Next
      End If
    End If
  Next
  CountUnique = n
End Function


But the UDF is like a formula that will all update for every change in the sheet.
So, I recommend you the macro, also the corrected macro.
It will work for all the rows, do the test and tell us how long it takes with the million lines.

VBA Code:
Sub count_uniques()
  Dim a As Variant, b As Variant
  Dim i As Long, n As Long, j As Long, jj As Long
  Dim dic As Object
 
  Set dic = CreateObject("Scripting.Dictionary")

  a = Range("A2:F" & Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 1)
  For i = 1 To UBound(a, 1) Step 2
    n = 0
    dic.RemoveAll
    For j = 1 To UBound(a, 2)
      If a(i, j) <> "" Then
        If Not dic.exists(a(i, j)) Then
          dic(a(i, j)) = Empty
          For jj = 1 To UBound(a, 2)
            If a(i + 1, jj) = a(i, j) Then
              n = n + 1
            End If
          Next
          b(i, 1) = n
        End If
      End If
    Next
  Next
  Range("G2").Resize(UBound(b)).Value = b
End Sub

I did a test with a million lines and it took 7 seconds.
:cool:
 
Last edited:
Upvote 0
the udf still wrong, see example,
but no offense dante, really, i need a simple if+countif and etc, as long as the formula will be,
again, if it's possible

test.xlsb
ABCDEFGH
1UDFsimplify
212345633
3456789
412232should be 2
52385
61234562
751
81234560
989
101234566
11123456
1212234should be 4
131223
14
1511214should be 4
161122
test
Cell Formulas
RangeFormula
G2,G12,G10,G8,G6,G4G2=CountUnique(A2:F2,A3:F3)
H2H2=COUNTIF(A2:F2,A3)+COUNTIF(A2:F2,B3)+COUNTIF(A2:F2,C3)+COUNTIF(A2:F2,D3)+COUNTIF(A2:F2,E3)+COUNTIF(A2:F2,F3)
E15E15=CountUnique(A15:D15,A16:D16)
 
Upvote 0
In your examples which one is having problems with the udf? I see all the results well. However, you want to fix the udf or a formula that uses countif.
If you don't want the udf, why are we wasting time fixing udf.
I'm not offended, I'm just giving you a better alternative.
 
Upvote 0
i've mistaken, in f15 should've wrote "should be 3",
and, with respect, i wrote in the first post i need a simple formula like with "if" and "countif" if possible,
but you offer macro+udf and i didn't want to ignore, so i checked and mention it's wrong, but i'm still in need of a simple formula
and after all, i sincerely appreciated your want to help, truly!
 
Upvote 0
Try this

Code:
Function CountUnique(r1 As Range, r2 As Range)
  Dim j As Long, jj As Long, n As Long
  Dim a As Variant, b As Variant
  Dim dic As Object
  a = r1.Value
  b = r2.Value
  Set dic = CreateObject("Scripting.Dictionary")

  For j = 1 To UBound(a, 2)
    If a(1, j) <> "" Then
      If Not dic.exists(a(1, j)) Then
        dic(a(1, j)) = Empty
        For jj = 1 To UBound(b, 2)
          If a(1, j) = b(1, jj) Then
            n = n + 1
            Exit For ' add
          End If
        Next
      End If
    End If
  Next
  CountUnique = n
End Function
 
Upvote 0
Or try

Book1
ABCDEFG
1smart way
21233322
3222333
Sheet1
Cell Formulas
RangeFormula
G2G2=COUNT(IF(MATCH(A2:F2,A2:F2,0)=COLUMN(A2:F2),MATCH(A2:F2,A3:F3,0)))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top