find last part of text

C

caveman.savant

seems easy but I can't figure it out
I want the last 13 sets of character (as numbers) representing monthly
occurances (the last being the total) of cell values in a1

ZXCVSL 1015L 04 ITEM UNLINED BOX C 0 3 0 2 1 15 2 2 0 0 0 0 24
ZXCVSL 1015L 04 ITEM UNLINED BOX 1 3 0 0 1 1 1 2 0 0 0 0 9

so that for with the 1st line, cell d1 = 0, e1=3, f1=0,g1=2....
and the 2nd line, cell d2 = 1, e2=3, f2=0,g2=0....
 
C

caveman.savant

seems easy but I can't figure it out
I want the last 13 sets of character (as numbers) representing monthly
occurances (the last being the total) of cell values in a1

ZXCVSL 1015L 04 ITEM UNLINED BOX C 0 3 0 2 1 15 2 2 0 0 0 0 24
ZXCVSL 1015L 04 ITEM UNLINED BOX 1 3 0 0 1 1 1 2 0 0 0 0 9

so that for with the 1st line, cell d1 = 0, e1=3, f1=0,g1=2....
and the 2nd line, cell d2 = 1, e2=3, f2=0,g2=0....

I came up with
=MID(MID(MID(SUBSTITUTE($A$5," ","^",((LEN(TRIM($A$5))-LEN(SUBSTITUTE
($A$5," ",""))+1)-13)+(COLUMN()-3)),1,256),FIND("^",SUBSTITUTE($A$5,"
","^",((LEN(TRIM($A$5))-LEN(SUBSTITUTE($A$5," ",""))+1)-13)+(COLUMN
()-3))),256),2,FIND(" ",MID(MID(SUBSTITUTE($A$5," ","^",((LEN(TRIM($A
$5))-LEN(SUBSTITUTE($A$5," ",""))+1)-13)+(COLUMN()-3)),1,256),FIND
("^",SUBSTITUTE($A$5," ","^",((LEN(TRIM($A$5))-LEN(SUBSTITUTE($A$5,"
",""))+1)-13)+(COLUMN()-3))),256))-2)

$A$5 refers to the cell with the line of text.
This works fine coping the fomula across 12 columns representing
months January to December.

If I try the copy it for the next row, I get an error. That's because
$A$5 reference stays locked in instead of becoming $A$6.

I try to get around this by naming A1 "top" and using =OFFSET(top,ROW
()-1,1) instead of the original $A$5 reference.

At this point I still get a #value error.

But I'm getting close.
 
B

Bernd P

Hello,

Take
=--RS(regexpreplace(RS($A1),"^\D*"&REPT("(\d+)\D+",13)&".*$","$13"))
for the first value (its 0).
=--RS(regexpreplace(RS($A1),"^\D*"&REPT("(\d+)\D+",13)&".*$","$12"))
for the second (its 3)
....
and
=--RS(regexpreplace(RS($A1),"^\D*"&REPT("(\d+)\D+",13)&".*$","$1"))
for the last (its 24)

RS and regexpreplace are UDF's which you have to copy into a macro
module:
Press ALT + F11, insert a new module, copy this text:
---- snip here ----
Function RS(Text As String) As String
RS = StrReverse(Text)
End Function
---- snip here ----
and regexpreplace from http://www.sulprobil.com/html/regexp.html into
the new module, go back to worksheet and press F9.

Regards,
Bernd
 
C

caveman.savant

Here's what i did.
1st I see that Bernie's crafting was only returning 12 values for me
not 13, the dropped value being the 1st.
so I adjusted it to read -1 on the second half. (my data contains #
signs so I used ^)
=MID(A2,FIND("^",SUBSTITUTE(A2," ","^",LEN(A2)-LEN(SUBSTITUTE(A2,"
",""))-(12)),1)-1,200)


I wanted to eliminate the exact cell reference (A2) so I named the A1
cell in the worksheet 'top' and started the data at A2. So then the
formula when...
=MID(OFFSET(top,ROW()-1,2),FIND("^",SUBSTITUTE(OFFSET(top,ROW()-1,2),"
","^",LEN(OFFSET(top,ROW()-1,2))-LEN(SUBSTITUTE(OFFSET(top,ROW
()-1,2)," ",""))-(12)),1)-1,200)


Finally with columns D - O representing January - December, I copied
this into each cell
=VALUE(MID(OFFSET(top,ROW()-1,2),SEARCH(CHAR(127),SUBSTITUTE(OFFSET
(top,ROW()-1,2)," ",CHAR(127),COLUMN()-4)),SEARCH(CHAR(127),SUBSTITUTE
((OFFSET(top,ROW()-1,2))," ",CHAR(127),COLUMN()-3))-SEARCH(CHAR
(127),SUBSTITUTE((OFFSET(top,ROW()-1,2))," ",CHAR(127),COLUMN()-4))))

I notice this doesn't work for the 1st set of numbers (January) so I
chanced that cell to
=VALUE(LEFT(OFFSET(top,ROW()-1,2),FIND(" ",OFFSET(top,ROW()-1,2))))
 
B

Bernie Deitrick

Try trimming the value in cell A2 - if you have a trailing space, it will
only return 12 values, not 13:

=MID(TRIM(A2),FIND("#",SUBSTITUTE(TRIM(A2),"
","#",LEN(TRIM(A2))-LEN(SUBSTITUTE(TRIM(A2)," ",""))-(12)),1)+1,200)

Bernie
 
B

Bernie Deitrick

Ooops. The formula shown broke on the space in the first SUSTITUTE function
argument: it should be this (make sure that the space is used)

=MID(TRIM(A2),FIND("#",
SUBSTITUTE(TRIM(A2)," ","#",
LEN(TRIM(A2))-LEN(SUBSTITUTE(TRIM(A2)," ",""))-(12)),1)+1,200)

Bernie
 
R

Ron Rosenfeld

seems easy but I can't figure it out
I want the last 13 sets of character (as numbers) representing monthly
occurances (the last being the total) of cell values in a1

ZXCVSL 1015L 04 ITEM UNLINED BOX C 0 3 0 2 1 15 2 2 0 0 0 0 24
ZXCVSL 1015L 04 ITEM UNLINED BOX 1 3 0 0 1 1 1 2 0 0 0 0 9

so that for with the 1st line, cell d1 = 0, e1=3, f1=0,g1=2....
and the 2nd line, cell d2 = 1, e2=3, f2=0,g2=0....

You could use a UDF:

<alt-F11> opens the VB Editor.
Ensure your project is highlighted in the project explorer window, then
Insert/Module and paste the code below into the window that opens.

Then use this formula in D1; fill right to P1 and fill down as far as required:

=RegexMid($A1,"\S+",RegexCount($A1,"\S+")-13+COLUMNS($A:A))

=====================================
Option Explicit
Function RegexMid(Str As String, Pattern As String, _
Optional Index As Variant = 1, _
Optional CaseSensitive As Boolean = True, _
Optional MultiLin As Boolean = False) _
As Variant 'Variant as value may be string or array


Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

Dim i As Long 'counter
Dim T() As String 'container for array results

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Set multiline
objRegExp.MultiLine = MultiLin

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.

On Error Resume Next 'return null string if a colmatch index is non-existent
If IsArray(Index) Then
ReDim T(1 To UBound(Index))
For i = 1 To UBound(Index)
T(i) = colMatches(Index(i) - 1)
Next i
RegexMid = T()
Else
RegexMid = CStr(colMatches(Index - 1))
If IsEmpty(RegexMid) Then RegexMid = ""
End If
On Error GoTo 0 'reset error handler
Else
RegexMid = ""
End If
End Function

Function RegexCount(Str As String, Pattern As String, _
Optional CaseSensitive As Boolean = True) As Long

Dim objRegExp As Object
Dim objMatch As Object
Dim colMatches As Object

' Create a regular expression object.
Set objRegExp = CreateObject("vbscript.regexp")

'Set the pattern by using the Pattern property.
objRegExp.Pattern = Pattern

' Set Case Insensitivity.
objRegExp.IgnoreCase = Not CaseSensitive

'Set global applicability.
objRegExp.Global = True

'Test whether the String can be compared.
If (objRegExp.Test(Str) = True) Then

'Get the matches.
Set colMatches = objRegExp.Execute(Str) ' Execute search.
RegexCount = colMatches.Count
Else
RegexCount = 0
End If
End Function
========================================
--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top