Pop Up Unique Values In Dialogue Box macro

nirvehex

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

I'm trying to write some Excel VBA code that when run, displays in a dialogue box, the unique # of values that I currently have selected. This should exclude things not highlighted especially when I am filtered.

Below is the code I have tried but doesn't work right:

<code>
Sub CountUniqueHighlightedCells()
Dim selectedRange As Range
Dim cell As Range
Dim uniqueValues As New Collection
Dim value As Variant
Dim count As Integer

' Check if any cells are selected
If Selection.Cells.Count = 0 Then
MsgBox "No cells selected!", vbExclamation
Exit Sub
End If

' Loop through each selected cell
For Each cell In Selection
' Check if the cell has a background color
If Not cell.Interior.ColorIndex = xlNone Then
' Add cell value to the collection if it's not already there
On Error Resume Next
uniqueValues.Add cell.Value, CStr(cell.Value)
On Error GoTo 0
End If
Next cell

' Count the number of unique values
count = uniqueValues.Count

' Display the count in a dialogue box
MsgBox "Number of unique values in highlighted cells: " & count, vbInformation
End Sub

</code>

Thank you!

Mark
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Please try this:

VBA Code:
Sub CountUniqueHighlightedCells()
  Dim selectedRange As Range
  Dim cell As Range
  Dim value As Variant
  Dim count As Integer
  Dim u As Range
  Dim Ary As Variant
  
  ' Check if any cells are selected
  If Selection.Cells.count = 0 Then
    MsgBox "No cells selected!", vbExclamation
    Exit Sub
  End If
  
  ' Loop through each selected cell
  For Each cell In Selection
    ' Check if the cell has a background color
    If Not cell.Interior.ColorIndex = xlNone Then
      If Not u Is Nothing Then
        Set u = Union(u, cell)
      Else
        Set u = cell
      End If
    End If
  Next cell
  If Not u Is Nothing Then
    Ary = Application.Unique(u)
    count = UBound(Ary)
  End If
  
  ' Display the count in a dialogue box
  MsgBox "Number of unique values in highlighted cells: " & count, vbInformation
End Sub
 
Upvote 0
Hi there - thanks for your help but this returns a value of 0 when I run it on a highlighted range that should be 10. Any other ideas?
 
Upvote 0
1. What do you mean by highlighted?
2. Do you want count only cells that are highlighted & visible?

3. Could you explain in more detailed?
Hi there - I select a range of cells could be filtered on not filtered, by highlighting them with my mouse. I would like a unique count of only what's visible and highlighted in the range. I'm not sure why Jeffrey's code is not working for me.
 
Upvote 0
I ran it several times and tested it and it worked fine for me
Hi Jeffrey - I'm not sure why it wouldn't be working. Here's an example of where it only returned a "1" for me. It should return "3."
 

Attachments

  • example.PNG
    example.PNG
    5.7 KB · Views: 10
Upvote 0
Hi there - I select a range of cells could be filtered on not filtered, by highlighting them with my mouse. I would like a unique count of only what's visible and highlighted in the range.
  1. So, it has nothing to do with cell's background color, right?
  2. Don't use reserved keyword as variable name, such as value, count etc.
  3. To get unique count, you can use dictionary object.
Try:
VBA Code:
Sub CountUniqueHighlightedCells()
Dim selectedRange As Range
Dim my_Cell As Range
Dim my_Value As Variant

' Check if any cells are selected
If Selection.Cells.CountLarge = 0 Then
MsgBox "No cells selected!", vbExclamation
Exit Sub
End If

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
' Loop through each selected cell
For Each my_Cell In Selection
    If my_Cell.EntireRow.Hidden = False Then  'if the cell is not hidden
        d(my_Cell.value) = Empty
    End If
Next

' Display the count in a dialogue box
MsgBox "Number of unique values in highlighted cells: " & d.count, vbInformation
End Sub
 
Upvote 0
  1. So, it has nothing to do with cell's background color, right?
  2. Don't use reserved keyword as variable name, such as value, count etc.
  3. To get unique count, you can use dictionary object.
Try:
VBA Code:
Sub CountUniqueHighlightedCells()
Dim selectedRange As Range
Dim my_Cell As Range
Dim my_Value As Variant

' Check if any cells are selected
If Selection.Cells.CountLarge = 0 Then
MsgBox "No cells selected!", vbExclamation
Exit Sub
End If

Set d = CreateObject("scripting.dictionary"): d.CompareMode = vbTextCompare
' Loop through each selected cell
For Each my_Cell In Selection
    If my_Cell.EntireRow.Hidden = False Then  'if the cell is not hidden
        d(my_Cell.value) = Empty
    End If
Next

' Display the count in a dialogue box
MsgBox "Number of unique values in highlighted cells: " & d.count, vbInformation
End Sub
That worked! Thank you so much!!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,259
Messages
6,177,480
Members
452,782
Latest member
ZCapitao

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