Counting unique and non-blank cells using a VBA code

sakate123

New Member
Joined
Aug 11, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am a new VBA user and I am trying to write a code to count values in a column. My data is dynamic and updates on a 24hr refresh and the data can be formatted slightly different each time it is updated. Typically the format of the values follows a pattern like this:
1597148799904.png

Where each alternating row is a blank. However, sometimes the data will have extra blank rows between the next occupied cell. (see below)
1597148892523.png

The values are always ascending, but each day at midnight the value resets to 1 and could potentially reach the same value that was already seen in the previous 24hrs. So a simple unique value formula will not work.
Eg. Aug 8, 2020 3:00AM: 110
Aug 9, 2020 1:15AM: 110

Basically what I need is a way to count every time the value increases while skipping over the blank cells (in pic 2 total count would be 3, exclude the 2nd 299).
I have a code that puts a value of 1 beside the cells with an increasing value and excludes any repetitive numbers but it does not work when there are extra blank cells in the data. Here is the code. (PiTag starts recording infomation in row 12)
1597149790551.png
 

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.
Welcome to the MrExcel board!

I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

So, IF you have Excel 365, try something like ..

VBA Code:
Sub Count_Unique()
  With Range("BQ1", Range("BQ" & Rows.Count).End(xlUp))
    MsgBox Evaluate(Replace("COUNT(UNIQUE(FILTER(#,#<>"""")))", "#", .Address))
  End With
End Sub
 
Upvote 0
Hi Peter,

Thanks for getting back to me. I have updated my account details.

Would you be able to explain the function a little more?
 
Upvote 0
I have updated my account details.
Thanks. (y)

Would you be able to explain the function a little more?
Take this example

sakate123 2020-08-11 1.xlsm
BQ
1299
2
3299
4299
5
6
7
8484
9
10
11485
12
Sheet1


With Range("BQ1", Range("BQ" & Rows.Count).End(xlUp))
This would be the range BQ1:BQ11 for the above sheet

Evaluate(Replace("COUNT(UNIQUE(FILTER(#,#<>"""")))", "#", .Address))
For that range this becomes
Evaluate("COUNT(UNIQUE(FILTER(BQ1:BQ11,BQ1:BQ11<>"""")))")

FILTER(BQ1:BQ11,BQ1:BQ11<>"""")
Return all the values from BQ1:BQ11 where the cell is not empty
{299,299,299,484,485}

UNIQUE({299,299,299,484,485})
Returns {299,484,485}

COUNT({299,484,485})
Returns 3
 
Upvote 0

Forum statistics

Threads
1,215,497
Messages
6,125,158
Members
449,208
Latest member
emmac

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