Count Distinct Value In Row Range

ibmy

Board Regular
Joined
Mar 4, 2020
Messages
121
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Sample of 100+k rows:
2.15.xlsb
FGHI
1Bexpected result :
21.21244
31.2124311
41.21244
51.21242
61.21241
71.21240
81.2123843
91.21237
101.21242
111.21240
121.21237
131.2124233
141.21241
151.21242
161.21237
171.2123721
181.21241
191.21241
201.21242
211.21242
221.21224
231.21224
241.2122453
251.21224
261.21225
271.21237
281.21225
291.21224
301.21225
311.21226
321.21226
total distinct value

Range = Start Range (Below Current Row : Row x+1) -> End Range (Current Row + Value in Column H)
Example in Row 8:
H8 value is 4 so Count Distinct Value in Colum F in range of : Row 8+1=9 -> Row 8+4=12
Result in Column I, Row 8, I8 which is 3
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this:
VBA Code:
Sub ibmy_1()

Dim i As Long, j As Long, n As Long
Dim va, vb
Dim d As Object

n = Range("F" & Rows.Count).End(xlUp).Row
va = Range("F2:F" & n)
vb = Range("H2:H" & n)
Set d = CreateObject("scripting.dictionary")

For i = 1 To UBound(va, 1)
    If vb(i, 1) <> "" Then
        For j = i + 1 To i + vb(i, 1)
            d(va(j, 1)) = Empty
            vb(i, 1) = d.Count
        Next
        i = i + vb(i, 1)
        d.RemoveAll
    End If
Next

Range("I2").Resize(UBound(vb, 1), 1) = vb
End Sub
 
Upvote 1
Solution
@Akuini ,thanks for the code.

May I know what line to change if value in Column H start at row 3.
 
Upvote 0
In Column H, at row 1&2 never exist a number, the earliest number exist is at row 3.

Is these 3 lines need to change? or I miss other line?

VBA Code:
va = Range("F3:F" & n)
VBA Code:
vb = Range("H3:H" & n)
VBA Code:
Range("I3").Resize(UBound(vb, 1), 1) = vb
 
Upvote 0
Why not create a Function and use it as a formula?
VBA Code:
Function UNIQUE_VALUES_IN_ROWS(ROWS_TO_COUNT As Long, COLUMN_START As Range) As Long
    Dim CountRange As Range
   
    Set CountRange = COLUMN_START.Offset(1, 0).Resize(ROWS_TO_COUNT, 1)
    UNIQUE_VALUES_IN_ROWS = WorksheetFunction.Count(WorksheetFunction.Unique(CountRange))
   
End Function

Then you could put the following formula in I8:
Excel Formula:
=UNIQUE_VALUES_IN_ROWS(H8,F8)
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,995
Members
449,480
Latest member
yesitisasport

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