NoDupes for numbers in just one cell using split

drom

Well-known Member
Joined
Mar 20, 2005
Messages
543
Office Version
  1. 2021
  2. 2019
  3. 2016
  4. 2013
  5. 2011
  6. 2010
  7. 2007
Hi and Thanks in advance!

I am using many times the following lines of code:

VBA Code:
Dim cNoDupes As New Collection
  Dim aCell():                aCell() = Selection.Value
Dim xAAA As Long:             xAAA = LBound(aCell())
Dim xZZZ As Long:             xZZZ = UBound(aCell())
Dim X As Long
Dim wTemp As String

    For X = xAAA To xZZZ
      wTemp = "":             wTemp = aCell(X)
      If wTemp <> "" Then
        cNoDupes.Add wTemp, CStr(wTemp)
      End If
    Next X
      Debug.Print cNoDupes.Count

works fine when I have text values in my selection

Now I would like to use a similar code for number, king of:

The prior code is the one I have been using for a range of cells

Now I am using just one cell, so I am trynf to use:


VBA Code:
Sub MyCodeForMyQuery()

  Dim aCell()
Dim cNoDupes As New Collection
  Dim wTemp As String
  Dim wCell As String:          wCell = "3,14,14,aa,aa,,aa,,,14,39,40,0, 0,0,a,aa,a,5,5,5,9,tt,t,45yyt" 'ActiveCell, for this eg enough
Dim vCell As Variant:           vCell = Split(wCell, ",")
  Dim xZbat As Long:            xZbat = UBound(vCell)
Dim xAAA As Single:             xAAA = LBound(vCell)
Dim xZZZ As Single:             xZZZ = UBound(vCell)
Dim xTemp As Single
Dim X As Single, XX As Single
                                If wCell = "" Or xZZZ = 0 Then Exit Sub
  
  For X = LBound(vCell) To UBound(vCell)
    wTemp = "":                 wTemp = vCell(X)
    xTemp = 0:                  xTemp = CSng(wTemp)
    If wTemp <> "" Then
      If xTemp > 0 Then
        XX = XX + 1:            ReDim Preserve aCell(1 To XX)
                                aCell(XX) = xTemp
        'Here is where I have Problems:
          cNoDupes.Add xTemp, CSng(xTemp)
      End If
    End If
  Next X



      Debug.Print cNoDupes.Count

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
How are you getting it to work without an On Error line ?
Also xTemp = CSng(wTemp) errors out when you pass it a Text value such as "aa".
This "cNoDupes.Add xTemp, CSng(xTemp)" has the Key as a numeric value it needs it to be a String

Give it a try replacing your for / next section with this:

Rich (BB code):
  For X = LBound(vCell) To UBound(vCell)
    wTemp = "":                 wTemp = vCell(X)
    xTemp = 0
    If wTemp <> "" And IsNumeric(wTemp) Then
      xTemp = CSng(wTemp)
      If xTemp > 0 Then
        XX = XX + 1:            ReDim Preserve aCell(1 To XX)
                                aCell(XX) = xTemp
        'Here is where I have Problems:
        On Error Resume Next
          cNoDupes.Add xTemp, CStr(xTemp)
        On Error GoTo 0
      End If
    End If
  Next X
 
Upvote 0
Solution
I have forgotten to add on my macro:
I usually put On error resume next (y)
The code was just fot this eg

I thought maeby was possible to add non string to a collection as non string
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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