+ Reply to Thread
Results 1 to 9 of 9

Extract Numbers from a string

Hybrid View

dugong Extract Numbers from a string 05-21-2008, 04:50 AM
dominicb Good morning dugong You... 05-21-2008, 05:19 AM
dugong Thanks DominicB - i see what... 05-21-2008, 05:32 AM
jindon try Sub test() With... 05-21-2008, 05:43 AM
dugong Thanks Jindon - but (if you... 05-21-2008, 05:49 AM
Simon Lloyd If you would rather use a... 05-21-2008, 06:04 AM
dugong Thanks Simon - I will look at... 05-21-2008, 06:21 AM
  1. #1
    Registered User
    Join Date
    10-23-2006
    Posts
    79

    Unhappy Extract Numbers from a string

    Hi

    I am really at a loss with VBA Code. I have looked on here and whilst the code supplied may do what i want, I cannot use the code if I dont understand what it is actually doing. So...(looking at the help!!!) I tried an attempt at pulling the numbers from a cell which may contain alphanumeric chars - What a mistake!

    The help say Val Function: returns the numbers contained in a string as a numeric value of appropriate type and gives an example. My attempt should loop through the cells in Column A and return the numbers in Column B)...until it gets to last row.... sooooo why wont this work..?
    Sub Test()
    
    Dim MyString
    Dim a As Integer, Numrows As Integer
    
    Numrows = Range("A65536").End(xlUp).Row               'Gets the Row count
    For a = 1 To Numrows
      MyString = Val(".Cells(a + 1, 1).Value")
      .Cells(a + 1, 2).Value = MyString
    Next a
    
    End Sub
    It bombs out at .Cells(a + 1, 2).Value = MyString saying invalid or unqualified reference.

    I really want to learn but find that the help will tell me something but when I apply it, it doesnt work!

    thanks again.. a struggling dugong!

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good morning dugong

    You need to amend the code like so :
    Sub Test()
    
    Dim MyString
    Dim a As Integer, Numrows As Integer
    
    Numrows = Range("A65536").End(xlUp).Row               'Gets the Row count
    For a = 1 To Numrows
      MyString = Val(Cells(a + 1, 1).Value)
      Cells(a + 1, 2).Value = MyString
    Next a
    
    End Sub
    You don't need the quotes in the brackets - this would only be required if you were feeding the text string directly from the code - you were building your own from column A.

    Not sure if this code does exactly what you want. The Val function returns all the numbers up to the first non numeric character, as the Excel helpfile demonstrates. If you want to extract all numeric characters you'd need to loop through each character in the cell.

    HTH

    DominicB
    Please familiarise yourself with the rules before posting. You can find them here.

  3. #3
    Registered User
    Join Date
    10-23-2006
    Posts
    79
    Quote Originally Posted by dominicb
    Not sure if this code does exactly what you want. The Val function returns all the numbers up to the first non numeric character, as the Excel helpfile demonstrates. If you want to extract all numeric characters you'd need to loop through each character in the cell....DominicB
    Thanks DominicB - i see what you mean. It only pulls out the numbers at the beginning!

    If I was to loop through each char, I understand I can use the LEN function to set an amount to loop through to but am unsure how to tell VBA how to recognise the first char in the cell to start with - does that make sense?

    Actually thinking aloud, maybe I could use the mid function to pull each char out and test it against 0 and 1-9 but how would I return the value in the next column..?

    dugong

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    try
    Sub test()
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d+(\.\d+)?"
        .Global = False
        For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
            If .test(r.Value) Then r.Offset(,1).Value = .execute(r.Value)(0)
        Next
    End With
    End Sub

  5. #5
    Registered User
    Join Date
    10-23-2006
    Posts
    79
    Quote Originally Posted by jindon
    try
    Sub test()
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d+(\.\d+)?"
        .Global = False
        For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
            If .test(r.Value) Then r.Offset(,1).Value = .execute(r.Value)(0)
        Next
    End With
    End Sub
    Thanks Jindon - but (if you have a minute of time ) can you explain what each part is doing..?
    As mentioned, I can't use the code - or adapt it - if I dont understand what it is actually doing.

    dugong

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834
    Quote Originally Posted by dugong
    Thanks Jindon - but (if you have a minute of time ) can you explain what each part is doing..?
    As mentioned, I can't use the code - or adapt it - if I dont understand what it is actually doing.

    dugong
    Then you can forget about my code...

    Try search the web with the keywords

    vba Regular Expression

    will give you the idea...
    Is this more understandable ?
    Sub test()
    Dim r As Range, i As Long
    For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
        If Len(r.Value) Then
            For i = 1 To Len(r.Value)
                If Val(Mid$(r.Value, i)) <> 0 Then
                    r.Offset(,1).Value =  Val(Mid$(r.Value, i))
                    Exit For
                End If
            Next
        End If
    Next
    End Sub

  7. #7
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    If you would rather use a worksheet formula then this has already been discussed and solved here
    Not all forums are the same - seek and you shall find

  8. #8
    Registered User
    Join Date
    10-23-2006
    Posts
    79
    Thanks Simon - I will look at that too!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1