Extract Numeric data and seperte with a comma

vgolla

New Member
Joined
Dec 15, 2014
Messages
7
ID
1254
4521
4021
IQID
4587
1251
125

<tbody>
</tbody>

I need a Macro that will extract only numeric data into a cell like ::: (1254,4521,4021,4587,1251,125)
with parenthesis and separated by commas.

Thanks for help.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Excel 2013
AB
1IDResult
21,254(1254, 4521, 4021, 4587, 1251, 125)
34521
44021
5IQID
64587
71251
8125
Sheet1


Try this:

Code:
[COLOR=#0000ff]Sub [/COLOR]Test()


    [COLOR=#0000ff]For[/COLOR] i = 2 [COLOR=#0000ff]To[/COLOR] 8
        [COLOR=#0000ff]If[/COLOR] IsNumeric(Cells(i, 1)) [COLOR=#0000ff]Then[/COLOR]
            [COLOR=#0000ff]If[/COLOR] i <> 8 [COLOR=#0000ff]Then[/COLOR]
                NewString = NewString & Cells(i, 1) & ", "
[COLOR=#0000ff]            Else[/COLOR]
                NewString = NewString & Cells(i, 1)
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        End If[/COLOR]
[COLOR=#0000ff]    Next [/COLOR][COLOR=#000000]i[/COLOR]
    Range("B2") = "(" & NewString & ")"
[COLOR=#0000ff]End Sub[/COLOR]
 
Last edited:
Upvote 0
Try this:

Code:
[COLOR=#0000ff]Sub [/COLOR]Test()


    [COLOR=#0000ff]For[/COLOR] i = 2 [COLOR=#0000ff]To[/COLOR] 8
        [COLOR=#0000ff]If[/COLOR] [B][COLOR=#FF0000]IsNumeric[/COLOR][/B](Cells(i, 1)) [COLOR=#0000ff]Then[/COLOR]
            [COLOR=#0000ff]If[/COLOR] i <> 8 [COLOR=#0000ff]Then[/COLOR]
                NewString = NewString & Cells(i, 1) & ", "
[COLOR=#0000ff]            Else[/COLOR]
                NewString = NewString & Cells(i, 1)
[COLOR=#0000ff]            End If[/COLOR]
[COLOR=#0000ff]        End If[/COLOR]
[COLOR=#0000ff]    Next [/COLOR][COLOR=#000000]i[/COLOR]
    Range("B2") = "(" & NewString & ")"
[COLOR=#0000ff]End Sub[/COLOR]
You may want to read my mini-blog article here...

<!-- title / author block --> [h=3]Thinking About Using VBA's IsNumeric Function? Read this first[/h]
 
Upvote 0
ID
1254
4521
4021
IQID
4587
1251
125

<tbody>
</tbody>

I need a Macro that will extract only numeric data into a cell like ::: (1254,4521,4021,4587,1251,125)
with parenthesis and separated by commas.
Give this macro a try...
Code:
Sub GetNumericIDs()
  Dim Cell As Range, IDs As String
  For Each Cell In Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlConstants, xlNumbers)
    If Not Cell.Value Like "*[!0-9]*" Then IDs = IDs & "," & Cell.Value
  Next
  Range("B2").Value = "'(" & Mid(IDs, 2) & ")"
End Sub
 
Upvote 0
Thanks Rick

I just took a gander at your blog article. I didn't realize all of the flaws.

I like the way you handle the commas! I suppose the mid(IDs,2) gets rid of the 1st preceding comma?

The one thing I didn't quite comprehend was this line:

Code:
If Not Cell.Value Like "*[!0-9]*" Then IDs = IDs & "," & Cell.Value

Is this saying if the cell value does not resemble numeric characters? I would think that this would return the opposite of what you want.

However, I tested it and of course it works! (Not really a surprise :))


So my question is how is it that this line of code operates the way it does? It seems counter intuitive.
 
Upvote 0
I like the way you handle the commas! I suppose the mid(IDs,2) gets rid of the 1st preceding comma?
Yes, I find it easier to do put the commas in first because mid is so simple to implement for removing the first character (especially since its third argument is optional defaulting to the remainder of the text when omitted)... putting the comma in last requires a combination of Left and Len (minus the length of the delimiter) and is more awkward in my opinion.


The one thing I didn't quite comprehend was this line:

Code:
If Not Cell.Value Like "*[!0-9]*" Then IDs = IDs & "," & Cell.Value

Is this saying if the cell value does not resemble numeric characters? I would think that this would return the opposite of what you want.

However, I tested it and of course it works! (Not really a surprise :))

So my question is how is it that this line of code operates the way it does? It seems counter intuitive.
It's a double negative (making a positive). If you tried to test it this way...

Code:
If Text Like "*[0-9]*" Then

all that would tell you is that at least one digit was in the text... it would say nothing about the other characters. Doing this...

Code:
If Text Like "*[!0-9]*" Then

would evaluate as true if at least one character was not a digit (that is what the exclamation point does... matches all characters not in the list), but that is not what you want the Then block to execute for... you want it to execute if none of the characters in Text are non-digits, to you apply the Not operator to get that as the True result.
 
Upvote 0
Anther option uses the ConcatIf UDF
Code:
Function ConcatIf(ByVal compareRange As Range, ByVal xCriteria As Variant, Optional ByVal stringsRange As Range, _
    Optional Delimiter As String, Optional NoDuplicates As Boolean) As String
    Dim i As Long, j As Long
    With compareRange.Parent
        Set compareRange = Application.Intersect(compareRange, Range(.UsedRange, .Range("a1")))
    End With
    If compareRange Is Nothing Then Exit Function
    If stringsRange Is Nothing Then Set stringsRange = compareRange
    Set stringsRange = compareRange.Offset(stringsRange.Row - compareRange.Row, _
    stringsRange.Column - compareRange.Column)
     
    For i = 1 To compareRange.Rows.Count
        For j = 1 To compareRange.Columns.Count
            If (Application.CountIf(compareRange.Cells(i, j), xCriteria) = 1) Then
                If InStr(ConcatIf, Delimiter & CStr(stringsRange.Cells(i, j))) <> 0 Imp Not (NoDuplicates) Then
                    ConcatIf = ConcatIf & Delimiter & CStr(stringsRange.Cells(i, j))
                End If
            End If
        Next j
    Next i
    ConcatIf = Mid(ConcatIf, Len(Delimiter) + 1)
End Function

with the formula =ConcatIf(A1:A10, ">0")
 
Upvote 0
Rick,

I believe I understand now. I thought it may have to do with something like a double negative.... but I was unable to see the second negative (!). Therefore, I was having trouble comprehending the solution. Thanks for the very thorough explanation! I appreciate you taking the time to explain the solution :)
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,978
Latest member
rrauni

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