VBA Script to Write Unique Value Count to a Cell

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
505
Office Version
  1. 365
Platform
  1. Windows
Hi,

I wrote a short code to get a unique count of a column, and I'm trying to get it to write that value in a certain cell. However, the last statement in my code is not correct where it says ActiveRange.Value = MyUnique.Count.

What am I doing wrong?

Code:
Sub SetupTest2()




'1. Select Pivot, Select B4, control+shift down, count filled rows, store count


Dim MyUnique As Object, c As Range


Set MyUnique = CreateObject("Scripting.Dictionary")


Sheets("Pivot").Select
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select


NumberofRows = Selection.Count


Sheets("Details").Select
Range("B3").Select
Range(Selection, Selection.End(xlDown)).Select


For Each c In Selection
        If c.EntireRow.Hidden = False Then MyUnique(CStr(c)) = 1
    Next c


With ActiveSheet
Range("B3").Select
Selection.End(xlDown).Select
Selection.Offset(2, 0).Select


ActiveRange.Value = MyUnique.Count


End With


End Sub

Thanks!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try
Code:
Selection.Value = MyUnique.Count
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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