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
 
f15 should've wrote "should be 3",
It is important that you explain the results, only you know the purpose.
Can you explain why 3?

1688738338415.png



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

Also with all due respect, but there it doesn't say that you want to change the formula.

i thought maybe some of you can help simplify it?
Simplify what, the formula or the UDF?

If you have slow problems on the page, the recommended thing is a macro, that's why I give you the macro.

But, let's try to solve your problem with the formula.

For that I need you to explain the results.
 
Last edited:
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
These are your formulas, one delivers 4 and the other 8, so explain how to get a result of 3.

Libro1
ABCDEFG
15112148
161122
Hoja1
Cell Formulas
RangeFormula
F15F15=SUM(--(COUNTIF(A16:D16,A15:D15)>0))
G15G15=COUNTIF(A15:D15,A16)+COUNTIF(A15:D15,B16)+COUNTIF(A15:D15,C16)+COUNTIF(A15:D15,D16)
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Phuoc - thank you but also wrong for the formula (didn't check the macro since i don't need it)

Dante - i apologize if i wasn't clear,
i post the macro for all who would like to help will see how i got to the result, since the xl2bb don't copy macro's,
but i meant simplify as in making a very simple formula, like the example i gave in h2,
and you are right, macro will be faster, but no convenient for me,

so explaining why 3: (i'll do my best)
i want to know how many numbers from the range a1:d1,
repeated uniquely from the range a2:d2
so when there's duplicates it get's tricky,
maybe possible to think like that ?
a1:d1 -
1a 1b 2a 1c
a2:d2 -
1a 1b 2a 2b
so in a1:d1 repeated only 1a and 1b and 2a but 1c didn't appear in a2:d2 so the total is 3
 
Upvote 0
No, that example is not the one I want you to explain. Explain the example in post#6, since you say in post#8 that F15 should be 3.

And by the way, explain the examples you have put in your post. since the example you tried to explain doesn't have duplicates.
 
Upvote 0
the example from post#6 in F15 identical to the one i gave in post#13,
and the examples i gave
Excel Formula:
=SUM(--(COUNTIF(A3:F3,A2:F2)>0))
is only an example for what i meant by "simplify" =
Excel Formula:
=COUNTIF(A2:F2,A3)+COUNTIF(A2:F2,B3)+COUNTIF(A2:F2,C3)+COUNTIF(A2:F2,D3)+COUNTIF(A2:F2,E3)+COUNTIF(A2:F2,F3)
which don't have duplicates but with duplicates this method don't work, and this is why i asked for help
basically in all examples, how many times, each number repeats, uniquely, in first row from the previous one
 
Upvote 0
I don't understand how to count when there are duplicates above and below.
It is something that you must explain, it is not enough to write "It should be..."

Please, write the result for each following case and a brief explanation for each case.

That will certainly help us help you.

Libro1
ABCDEFG
1Result
212341
31756
4
51234?why
61156
7
81234?why
91116
10
111234?why
121111
13
141134?why
151256
16
171134?why
181156
19
201134?why
211116
22
231134?why
241111
25
261114?why
271567
28
291114?why
301167
31
321114?why
331117
34
351114?why
361111
37
381111?why
391567
40
411111?why
421167
43
441111?why
451117
46
471111?why
481111
Hoja1


;)
 
Upvote 0
trying...

test
ABCDEFGH
1Result
212341
31756
4
512341whythe number "1" in a5 :d5 repeated uniquely from a6:d6
61156
7
812341whythe number "1" in a8 :d8 repeated uniquely from a9:d9
91116
10
1112341whythe number "1" in a11:d11 repeated uniquely from a12:d12
121111
13
1411341whythe number "1" in a14:d14 repeated uniquely from a15:d15
151256
16
1711342whythe number "1, 1" in a17:d17 repeated uniquely from a18:d18
181156
19
2011342whythe number "1, 1" in a20:d20 repeated uniquely from a21:d21
211116
22
2311342whythe number "1, 1" in a23:d23 repeated uniquely from a24:d24
241111
25
2611141whythe number "1" in a26:d26 repeated uniquely from a27:d27
271567
28
2911142whythe number "1, 1" in a29:d29 repeated uniquely from a30:d30
301167
31
3211143whythe number "1, 1, 1" in a32:d32repeated uniquely from a33:d33
331117
34
3511143whythe number "1, 1, 1" in a35:d35 repeated uniquely from a36:d36
361111
37
3811111whythe number "1" in a38:d38 repeated uniquely from a39:d39
391567
40
4111112whythe number "1, 1" in a41:d41 repeated uniquely from a42:d42
421167
43
4411113whythe number "1, 1, 1" in a44:d44 repeated uniquely from a45:d45
451117
46
4711114whythe number "1, 1, 1, 1" in a47:d47 repeated uniquely from a48:d48
481111
test
Cell Formulas
RangeFormula
F2,F47,F44,F41,F38,F35,F32,F29,F26,F23,F20,F17,F14,F11,F8,F5F2=CountUnique(A2:D2,A3:D3)
 
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