Hello everyone,
I have the following question:
In a workbook I have a few rows looking like this:
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: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
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.
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.
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
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.
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
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
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?
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
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
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
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:
I kinda understand what it does, but I'm not sure what the "^", "+", etc. mean..Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
Thank you!
This seems to work as well! Thanks for your response.
Could you also please add notes like I asked StephenR, especially for this line:
Same here, I kinda understand what it does, but not everything..Pattern = "\b21\d{4}\b"
Thanks in advance,
Marco
There are plenty of websites on regular expressions. It's quite an involved but powerful method for matching text strings. In outline:
Says look either at the beginning of the cell or for any characters which are not numbers (which may or may not be there)(^|[^0-9]+)
Says look for 21 followed by 4 numbers (this is the bit we want)(21[0-9]{4})
says look either at the end of the cell or for any characters which are not numbers (which may or may not be there).($|[^0-9]+)
nilem's code works on a similar principle, but he his code is more succinct.
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.
I removed the first "^" in the following line: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
Why does this work? And is it a good way to solve it? What difference does "^|" make over just the "|"?.Pattern = "(^|[^0-9]+)(21[0-9]{4})($|[^0-9]+)"
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks