+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Searching value of specific length

  1. #1
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Searching value of specific length

    Hello everyone,

    I have the following question:

    In a workbook I have a few rows looking like this:

     LISDFGSD CD                          SDGSDFGSDFPLEIN 33 A 3445 EP ROTT 21555                         210521/MIV 16-3-2010/DJ
    
     DEBI 213405             IAS        PLEINPLEIN 21 B 30e5 EP ROTT 18112825
    There are lots of spaces, not consistent at all. Lets say the first line is A1 and the second A2. What I need is a formula in the cell next to these two lines that give me the following values:
    For A1: 210521
    For A2: 213405

    The formula has to subtract a number which is always 6 digits long, starting with "21". It can be anywhere in the cell.

    I hope I'm clear enough explaining this!

    Thanks in advance,

    Marco

    Edit: SOLVED
    Last edited by Marco-Kun; 12-06-2011 at 10:24 AM.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    Sorry, where does the 210521 come from? Subtract what from what?

    OK, I see now, it was off the edge of the code window.
    Last edited by StephenR; 12-01-2011 at 06:51 AM.

  3. #3
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Quote Originally Posted by StephenR View Post
    Sorry, where does the 210521 come from? Subtract what from what?

    OK, I see now, it was off the edge of the code window.
    I see the problem. I thought about removing the spaces but since they are there in the workbook I don't know what the effect would be in a formula. So I left it at the original form.

    There is no way to enlarge the code box, is there?

    I hope you can help me with this question.

    Marco

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    I'm sure this can be done with formulae, but I would use VBA - will come back. Here is a user-defined formula, so you would enter =x(A1) in B1 and so on. Suggest you change the name to something more meaningful:
    Function x(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "21[0-9]{4}"
        If .Test(rCell) Then
            x = .Execute(rCell)(0)
        Else
            x = ""
        End If
    End With
    
    End Function
    Last edited by StephenR; 12-01-2011 at 07:02 AM.

  5. #5
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Thank you, this works! But there is one thing I'm worried about.
    I tested this for cell A1. The extracted value should be 210521.
    But whenever I add more number to the value, like: 21052111111, it still subtract 210521, which shouldn't be the case. It should only be subtracted when the length is exactly 6. Could this be achieved?

    Thanks in advance,

    Marco

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    I think this works:
    Function x(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "([^0-9]+)(21[0-9]{4})([^0-9]+)"
        If .Test(rCell) Then
            x = .Execute(rCell)(0).SubMatches(1)
        Else
            x = ""
        End If
    End With
    
    End Function

  7. #7
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Quote Originally Posted by StephenR View Post
    I think this works:
    Function x(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "([^0-9]+)(21[0-9]{4})([^0-9]+)"
        If .Test(rCell) Then
            x = .Execute(rCell)(0).SubMatches(1)
        Else
            x = ""
        End If
    End With
    
    End Function
    Thanks again for the quick reply. It seems to work for a few rows.
    I added an example with above VBA, the output and a column with the number which it should be. I hope this helps!

    Marco
    Attached Files Attached Files

  8. #8
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Sorry for the double post, but I discovered the following.
    The numbers which doesn't get subtracted from the rows all are the end of the data in that cell. Whenever I add a space to the end it does get subtracted, but that shouldn't be necessary. Any idea?

  9. #9
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    Try changing the two '+' to * in the Pattern line.

  10. #10
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Quote Originally Posted by StephenR View Post
    Try changing the two '+' to * in the Pattern line.
    Although this works, the previous problem comes back again where it subtracts numbers containing 7 digits which start with 21, which it shouldn't do. I added a new example. Cell A9 in that sheet contains data with a number which shouldn't be subtracted, however, it does.

    Thank you.

    Marco
    Attached Files Attached Files

  11. #11
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    Ah yes missed that. This seems to work:
    Function DEB(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
        If .Test(rCell) Then
            DEB = .Execute(rCell)(0).SubMatches(1)
        Else
            DEB = ""
        End If
    End With
    
    End Function

  12. #12
    Valued Forum Contributor
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    Excel 2010
    Posts
    522

    Re: Searching value of specific length

    may be so (as an option)
    With CreateObject("VBScript.RegExp")
        .Pattern = "\b21\d{4}\b"
        If .Test(rCell) Then DEB = .Execute(rCell)(0) Else DEB = ""
    End With

  13. #13
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Quote Originally Posted by StephenR View Post
    Ah yes missed that. This seems to work:
    Function DEB(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
        If .Test(rCell) Then
            DEB = .Execute(rCell)(0).SubMatches(1)
        Else
            DEB = ""
        End If
    End With
    
    End Function
    Thank you very much! That works indeed!
    I have one little question though. Could you please add notes with ' explaining in short what the code does exactly? Especially this line:
        .Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
    I kinda understand what it does, but I'm not sure what the "^", "+", etc. mean.
    Thank you!

    Quote Originally Posted by nilem View Post
    may be so (as an option)
    With CreateObject("VBScript.RegExp")
        .Pattern = "\b21\d{4}\b"
        If .Test(rCell) Then DEB = .Execute(rCell)(0) Else DEB = ""
    End With
    This seems to work as well! Thanks for your response.
    Could you also please add notes like I asked StephenR, especially for this line:
        .Pattern = "\b21\d{4}\b"
    Same here, I kinda understand what it does, but not everything.

    Thanks in advance,

    Marco

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    3,788

    Re: Searching value of specific length

    There are plenty of websites on regular expressions. It's quite an involved but powerful method for matching text strings. In outline:
    (^|[^0-9]+)
    Says look either at the beginning of the cell or for any characters which are not numbers (which may or may not be there)
    (21[0-9]{4})
    Says look for 21 followed by 4 numbers (this is the bit we want)
    ($|[^0-9]+)
    says look either at the end of the cell or for any characters which are not numbers (which may or may not be there).

    nilem's code works on a similar principle, but he his code is more succinct.

  15. #15
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:Excel 2007
    Posts
    159

    Re: Searching value of specific length

    Thanks for your help StephenR!
    Now another thing regarding this came around.
    First the condition was that no numbers could be infront the "21". Now something changed and it seems numbers are no problem.
    I was trying things out and I removed a "^" in the code, making it work.

    Function DEB(rCell As Range) As Variant
    
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "(|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
        If .Test(rCell) Then
            DEB = .Execute(rCell)(0).SubMatches(1)
        Else
            DEB = ""
        End If
    End With
    
    End Function
    I removed the first "^" in the following line:

        .Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
    Why does this work? And is it a good way to solve it? What difference does "^|" make over just the "|"?

    Another question. Although it rarely happens, the format could also be like this:
    0000 0000 0021 1234

    Is there a way to adjust to code to either look for a space and than 4 numbers, just 4, no 5, or look as the code already works, 4 adjacent numbers without a space?

    Thanks for all your help!

    Marco

+ 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.2.0