+ Reply to Thread
Results 1 to 5 of 5

Extract Digit Function Question

  1. #1
    rrstudio2@icqmail.com
    Guest

    Extract Digit Function Question

    I found a function which seperates out numbers from letters. I
    modified the formula to keep decimal points in. The formula works
    well, however, the function came with a line that I don't understand.
    The whole function is
    ------------------------------
    Function ExtractDigits(cell As String) As Variant
    'extract 1st continuous set of digits
    'David McRitchie, 2001-09-26
    Dim i As Long, flag As Long
    flag = 0
    ExtractDigits = ""
    For i = 1 To Len(cell)
    If Mid(cell, i, 1) >= "0" And _
    Mid(cell, i, 1) <= "9" Or _
    Mid(cell, i, 1) = "." Then
    flag = 1
    ExtractDigits = ExtractDigits & Mid(cell, i, 1)
    'ExtractDigits = ExtractDigits * 1 'not sure what this does?
    but 12.3=123 with on
    Else
    If flag = 1 Then Exit Function
    End If
    Next i
    End Function
    -----------------------------
    I don't see what the purpose of the ExtractDigits = ExtractDigits * 1
    is and I don't get what it is doing. If it is enabled, like it was in
    the original formula, then if the cell is 12.3mg it returns 123 while
    if it is disabled, then it returns 12.3. How is this line removing the
    decimal point?

    Thanks,
    Andrew V. Romero

    PS: Sorry if this message comes thru multiple times, Google groups
    seems to be acting up.


  2. #2
    rrstudio2@icqmail.com
    Guest

    Re: Extract Digit Function Question

    Ignore, see the slightly older thread. There was apparently over an
    hour delay in posting messages this morning....
    -Andrew


  3. #3
    Tom Ogilvy
    Guest

    Re: Extract Digit Function Question

    Testing your function with the *1 in it does this

    ? ExtractDigits("abcd12.3efgh")
    12.3

    so the problem is (besides your post with 4 responses which you ignored and
    wasted people's time - however, that is an indicator) that you screwed up
    and formatted your cell not to show decimal places.

    It works without the *1 because then the function returns a text string and
    as many know, a text string is not formatted by a number format.

    When you apply the *1, it converts the result to a number which is affected
    by the formatting of the cell. Format your cell to general and a light bulb
    should come on.

    In any event, Ron Rosenfeld suggested a much simpler function which should
    work for the situation you describe (but it would also be affected by cell
    formatting).

    --
    Regards,
    Tom Ogilvy


    <rrstudio2@icqmail.com> wrote in message
    news:1135716723.675910.166210@g44g2000cwa.googlegroups.com...
    > I found a function which seperates out numbers from letters. I
    > modified the formula to keep decimal points in. The formula works
    > well, however, the function came with a line that I don't understand.
    > The whole function is
    > ------------------------------
    > Function ExtractDigits(cell As String) As Variant
    > 'extract 1st continuous set of digits
    > 'David McRitchie, 2001-09-26
    > Dim i As Long, flag As Long
    > flag = 0
    > ExtractDigits = ""
    > For i = 1 To Len(cell)
    > If Mid(cell, i, 1) >= "0" And _
    > Mid(cell, i, 1) <= "9" Or _
    > Mid(cell, i, 1) = "." Then
    > flag = 1
    > ExtractDigits = ExtractDigits & Mid(cell, i, 1)
    > 'ExtractDigits = ExtractDigits * 1 'not sure what this does?
    > but 12.3=123 with on
    > Else
    > If flag = 1 Then Exit Function
    > End If
    > Next i
    > End Function
    > -----------------------------
    > I don't see what the purpose of the ExtractDigits = ExtractDigits * 1
    > is and I don't get what it is doing. If it is enabled, like it was in
    > the original formula, then if the cell is 12.3mg it returns 123 while
    > if it is disabled, then it returns 12.3. How is this line removing the
    > decimal point?
    >
    > Thanks,
    > Andrew V. Romero
    >
    > PS: Sorry if this message comes thru multiple times, Google groups
    > seems to be acting up.
    >




  4. #4
    Tom Ogilvy
    Guest

    Re: Extract Digit Function Question

    for completeness, I had changed the location of the *1 - but the formatting
    is another possibility.

    Function ExtractDigits(cell As String) As Variant
    'extract 1st continuous set of digits
    'David McRitchie, 2001-09-26
    Dim i As Long, flag As Long
    flag = 0
    ExtractDigits = ""
    For i = 1 To Len(cell)
    If Mid(cell, i, 1) >= "0" And _
    Mid(cell, i, 1) <= "9" Or _
    Mid(cell, i, 1) = "." Then
    flag = 1
    ExtractDigits = ExtractDigits & Mid(cell, i, 1)
    Else
    If flag = 1 Then Exit Function
    End If
    Next i
    ExtractDigits = ExtractDigits * 1
    End Function

    --
    Regards,
    Tom Ogilvy


    "Tom Ogilvy" <twogilvy@msn.com> wrote in message
    news:%23dDttKzCGHA.3036@TK2MSFTNGP10.phx.gbl...
    > Testing your function with the *1 in it does this
    >
    > ? ExtractDigits("abcd12.3efgh")
    > 12.3
    >
    > so the problem is (besides your post with 4 responses which you ignored

    and
    > wasted people's time - however, that is an indicator) that you screwed up
    > and formatted your cell not to show decimal places.
    >
    > It works without the *1 because then the function returns a text string

    and
    > as many know, a text string is not formatted by a number format.
    >
    > When you apply the *1, it converts the result to a number which is

    affected
    > by the formatting of the cell. Format your cell to general and a light

    bulb
    > should come on.
    >
    > In any event, Ron Rosenfeld suggested a much simpler function which should
    > work for the situation you describe (but it would also be affected by cell
    > formatting).
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > <rrstudio2@icqmail.com> wrote in message
    > news:1135716723.675910.166210@g44g2000cwa.googlegroups.com...
    > > I found a function which seperates out numbers from letters. I
    > > modified the formula to keep decimal points in. The formula works
    > > well, however, the function came with a line that I don't understand.
    > > The whole function is
    > > ------------------------------
    > > Function ExtractDigits(cell As String) As Variant
    > > 'extract 1st continuous set of digits
    > > 'David McRitchie, 2001-09-26
    > > Dim i As Long, flag As Long
    > > flag = 0
    > > ExtractDigits = ""
    > > For i = 1 To Len(cell)
    > > If Mid(cell, i, 1) >= "0" And _
    > > Mid(cell, i, 1) <= "9" Or _
    > > Mid(cell, i, 1) = "." Then
    > > flag = 1
    > > ExtractDigits = ExtractDigits & Mid(cell, i, 1)
    > > 'ExtractDigits = ExtractDigits * 1 'not sure what this does?
    > > but 12.3=123 with on
    > > Else
    > > If flag = 1 Then Exit Function
    > > End If
    > > Next i
    > > End Function
    > > -----------------------------
    > > I don't see what the purpose of the ExtractDigits = ExtractDigits * 1
    > > is and I don't get what it is doing. If it is enabled, like it was in
    > > the original formula, then if the cell is 12.3mg it returns 123 while
    > > if it is disabled, then it returns 12.3. How is this line removing the
    > > decimal point?
    > >
    > > Thanks,
    > > Andrew V. Romero
    > >
    > > PS: Sorry if this message comes thru multiple times, Google groups
    > > seems to be acting up.
    > >

    >
    >




  5. #5
    rrstudio2@icqmail.com
    Guest

    Re: Extract Digit Function Question

    Thanks for the information. It is (as Bob Phillips mentioned in my
    other post- again sorry for the multple posting, no messages were being
    posted for about 2 hrs) that each digit was being multiplied by 1 so
    the 12. * 1 = 12, then the next digit came along 123*1=123. I am
    pretty familier with typical excel things, such as formatting, but am
    just starting to get a grasp of the power of VB in excel. Looking
    foward to learning more about it. Hopefully soon I will be able to be
    creating my own instead of modifying other's functions.

    -Andrew V. Romero


+ 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