Get frequency of elements in Range

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a spreadsheet with error codes (column A) and timestamps (column B) . Every time there is an error, it is recorded and time stamped. I would like to write a macro that writes in another column (column D) the frequency of each error. I have filled column C with the distinct error codes possible, and I would like to go through column A and count the occurrence of each code in column C.

However, there are about 1 000 error codes possible and about 75 000 timestamped values, so I would like to avoid going through all 75 000 values for every one of the 1 000 codes possible. I there a better way to do this?

Thanks
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi, @always_confused
so I would like to avoid going through all 75 000 values for every one of the 1 000 codes possible
I have a code to do that.
I assumed your data is sorted by col B.
Could you sort your data by col A? you can sort it back by col B after running the code.

VBA Code:
Sub a1167835a()
'https://www.mrexcel.com/board/threads/get-frequency-of-elements-in-range.1167835/
Dim i As Long, j As Long, k As Long
Dim va, vb

va = Range("A1", Cells(Rows.Count, "A").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    
        i = i - 1
        k = k + 1
        vb(k, 1) = va(j, 1)
        vb(k, 2) = i - j + 1

Next

'put result in col D:E
Range("D2:E10000").ClearContents
Range("D2").Resize(k, 2) = vb

End Sub

Example:
Book1
ABCDE
1codecodefrequency
2GG9
3GK4
4GT4
5G
6G
7G
8G
9G
10G
11K
12K
13K
14K
15T
16T
17T
18T
Sheet5


It doesn't need the 1000 error codes possible, it just count the frequency of code in col A.
I tested it on 100K rows of data, it took a split second to finish.
 
Upvote 0
Solution
@Akuini That works great, thank you so much. Do you know how I could sort both columns to have the most frequent error and its frequency at the top?
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
Hello again,

Turns out I'm having an issue with this bit of code. I have adjusted it a bit to fit my sheet and when I fill the column with the frequencies, there is an extra value that doesn't belong to any code. For 1 000 codes, I get 1 001 frequencies. I'm not sure what this is or where it's coming from.

This is the codes as I have modified it

VBA Code:
Sub a1167835a()
Dim i As Long, j As Long, k As Long
Dim va, vb

va = Range("F1", Cells(Rows.Count, "F").End(xlUp))
ReDim vb(1 To UBound(va, 1), 1 To 2)

For i = 2 To UBound(va, 1)
    j = i
    Do
        i = i + 1
        If i > UBound(va, 1) Then Exit Do
    Loop While va(i, 1) = va(i - 1, 1)
    
        i = i - 1
        k = k + 1
        vb(k, 1) = va(j, 1)
        vb(k, 2) = i - j + 1

Next
Range("P2:Q10000").ClearContents
Range("P2").Resize(k, 2) = vb

End Sub

I end up with an extra 1500 value at the end of the list of frequencies.
 
Upvote 0
I've tacked
VBA Code:
lastrow = Range("Q" & Rows.Count).End(xlUp).Row
Range("Q" & lastrow).Clear

Onto the end, so now the output is ok, but I'd prefer not to assume it's wrong and correct and instead know what's going wrong and prevent it....
 
Upvote 0
when I fill the column with the frequencies, there is an extra value that doesn't belong to any code. For 1 000 codes, I get 1 001 frequencies. I'm not sure what this is or where it's coming from.
I end up with an extra 1500 value at the end of the list of frequencies.
Did you sort data by col A before running the code?
If yes then it means that there are more than 1000 unique code in col A.
Is it possible you have trailing space in your data?
I couldn't test it without your original data.
 
Upvote 0
Did you sort data by col A before running the code?
If yes then it means that there are more than 1000 unique code in col A.
Is it possible you have trailing space in your data?
I couldn't test it without your original data.
Oh yes thanks, hadn't realized but an issue elsewhere had added a bunch of extra rows. Fixed it and now it works again. Thank you ?
 
Upvote 0

Forum statistics

Threads
1,214,929
Messages
6,122,317
Members
449,081
Latest member
tanurai

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