+ Reply to Thread
Results 1 to 15 of 15

Thread: Extraction of values following a pattern

  1. #1
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Extraction of values following a pattern

    Hi,

    I have the following problem that I don't succeed to solve.
    I actually couldn't find a correct way to describe it in the title.

    It is easier for me to summarize it with the following example:

    ----- are just here act as a separator between columns A and B
    All the letters are in column A
    All the numbers are in column B

    I'm looking for a way to find where is AAA (it won't be the first line), then to find the next line where is located the exact value "owned", then to take the value in the column B for this same line.
    Then to repeat the same process for BBB CCC ...

    I don't have the same numbers of lines between "XXX" and "owned"


    AAA
    da
    wf
    t3
    owned ------ 26
    BBB
    fq
    sdf
    wt
    vreg
    erqtg
    owned ------- 12
    CCC
    sf
    sdf
    owned -------- 23

    The result that I expect is like this:
    AAA 26
    BBB 12
    CCC 23
    … …
    in two columns.

    I know (more or less) how to use VLOOKUP.
    I'm mainly stuck for everything else regarding this problem.

    Any help to solve it would be greatly appreciated, or at least to be directed to the exact formulas that I will have to use.

    Thank you.
    Last edited by gvaltat; 10-30-2011 at 05:57 PM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    Strings in column A.
    Values in column B.

    AAA in D1, this formula in E1:

    =VLOOKUP("owned", OFFSET(INDEX(A:A, MATCH(D1,A:A, 0)),,,COUNTA(A:A),2), 2, 0)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    Thank you Jerry for your quick answer. I really appreciate it.
    It works perfectly, and it is very nice to learn from a real problem.

    Only my own understanding is not as perfect as your solution.
    I'm trying to adapt it to a more complex problem...and I'm miserably failing so far.
    I will probably come with more questions later.

    Regards,
    Guillaume

  4. #4
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    This part of the formula is finding the "row" the value of D1 is on by searching column A:

    MATCH(D1,A:A, 0)

    Then adding that to the INDEX results in a cell value:

    INDEX(A:A, MATCH(D1,A:A, 0))

    We then insert that as the first parameter of an OFFSET() function, and expand the result to start from that cell downward, and expand to two columns.

    OFFSET(INDEX(A:A, MATCH(D1,A:A, 0)),,,COUNTA(A:A),2)

    That creates a two-column range in A:B that starts at the value from D1, the goes down. All of that is inserted into a standard VLOOKUP to the get "owned" row and the adjacent value:

    =VLOOKUP("owned", OFFSET(INDEX(A:A, MATCH(D1,A:A, 0)),,,COUNTA(A:A),2), 2, 0)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  5. #5
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    Thank you for the follow up, I can see why you are a "guru"
    Abstractly, I understand what you did, my problem is to master it enough to include it in a wider problem.
    I thought wrongly that I would be able to extrapolate from a easier example, which is not the case so far.
    What I named AAA is actually more complicated.
    Here is the exact first step of my problem:
    tag	AAA		
    fg	erg			
    14	{	dfb		
    =	vdv	45		
    flag	AAA			
    flag	BBB			
    erg	>			
    owned	12	23	456	13
    				
    				
    				
    tag	BBB			
    flag	AAA			
    flag	CCC			
    wdh	546			
    {	2	}		
    owned	56	2
    ...
    The result I'm looking for is:
    AAA	12	23	456	13
    BBB	56	2
    So, what I'm looking for is to have both "tag" in cell An and "AAA" in cell Bn, then look for the first occurrence of "owned" in the following lines, and extract whatever numbers (they will be only integers) are on this line.

    To explain the entire problem, the "owned" values are IDs (hence unique).
    AAA, BBB are "owners"
    An ID have so far one and only one owner (but I plan to change this to "at least one owner").

    My final goal is to convert the result above into the following way, in order to convert it into a txt file, then to import it into a GIS (Geographical Information System)

    1	AAA
    2	CCC
    3	AAA
    4	AAA
    5	FFF
    6	KKK
    ...	...
    For the first step, I guess that I have to insert a boolean (for "tag" AND "AAA") inside INDEX, but I don't see how it is possible.

  6. #6
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    As a formula, that's only a small tweak. Moving over to column K to give room for those extra columns of data...

    Then with AAA in K1, the first formula in L1 would be:

    =VLOOKUP("owned", OFFSET(INDEX($A:$A, MATCH($K1,$B:$B, 0)),,,COUNTA($A:$A),10), COLUMN(B1), 0)

    I added a new function COLUMN() to increment the returned column value as you copy the formula to the right.

    This project of yours is complex enough I'd suggest using VBA might be better.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  7. #7
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    My apologies... I didn't notice the "tag" requirement, too. This definitely a magnitude of complexity harder for a "formula", and with this formula below you may start to notice "lag" in your sheet as they calculate. I only set the range for 1000 cells, hopefully that's enough.

    =VLOOKUP("owned", OFFSET(INDEX($A$1:$A$1000, MATCH("tag" & $K1, INDEX($A$1:$A$1000&$B$1:$B$1000, 0), 0)),,,COUNTA($A:$A),10), COLUMN(B1), 0)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  8. #8
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    Truly, no need to apologize, Sir.
    I will look at this this week and will keep you informed.
    The lag won't be a problem as long as the result won't be a crash (It is more like 400k lines, that hopefully excel 2007 will manage).
    Anyway, I'm aware that I will have to learn VBA at some point.
    For now, I'm learning a lot from your help.

    Thank you again,
    Guillaume

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    I wouldn't process 400k of data this way. When there's this much data, I'd add a "key" column to create an easy-match tag system.

    Here's a sheet showing a much simpler standard VLOOKUP using an added new column A to make the lookups easy. Just add that column to your real dataset, copy formula2 all the way down.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  10. #10
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    Ok, I understand why you are doing it this way.
    The good point: It works with a small file, and I've learned a lot. T
    The bad point: Even this won't work with a file the size of mine (txt file of 15 Mo before import). After import, I can't even insert a new column in column A. It was my real first try with Excel 2007, and I wrongly hoped that the extra-capacity (compared to the previous version) would be enough.

    I know nothing about VBA. Do you think that it would be enough to solve this problem? If the answer is positive, I will start looking at it.
    Thank you again for the help.
    Guillaume

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    I'm intrigued. Why would you be unable to insert a column A?

    VBA can do this quite easily. That much data would take a bit of time, but by no means hard to write.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    I get the following message:
    Excel cannot complete this task with available resources. Choose less data or close other applications.
    There is nothing that I can close that would substantially increase the memory available.
    I have no trouble to add a column to the right, but I can't to the left.

    Which made me realized that the import has created columns from A to XFD (when A to ABV was enough), which implies an impossible amount of (empty) data to shift.

    I have the same message when I try to copy-paste the relevant part in a (A->ABV) sheet.
    I've tried previously to remove the useless column, but I've not found if it is possible to do so.

    I will follow your advice, and will start to look to VBA.

  13. #13
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    Can you select all the columns ABW:XFD and do an Edit > Clear > All ?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  14. #14
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,210

    Re: Extraction of values following a pattern

    Here's a little macro that can extract the data to a separate sheet. The macro is in the Module1 of the VBEditor.

    Option Explicit
    
    Sub CollectTagInfo()
    Dim tagFIND As Range, tagNEXTFIND As Range, tagOWNED As Range
    Dim wsData As Worksheet, wsOUT As Worksheet, NR As Long
    
    If MsgBox("Process this active sheet's data?", vbYesNo, "Confirm") _
                                = vbNo Then Exit Sub
    Set wsData = ActiveSheet
    
    With wsData
        .Rows(1).Insert xlShiftDown
        On Error Resume Next
        Set tagFIND = .Range("A:A").Find("tag", LookIn:=xlValues, LookAt:=xlWhole)
        
        If tagFIND Is Nothing Then
            MsgBox "'TAG' flag not found in column A, please recheck the data"
            .Rows(1).Delete xlShiftUp
            Exit Sub
        End If
        
        Application.ScreenUpdating = False
        Set wsOUT = Sheets.Add(After:=Sheets(Sheets.Count))
        wsOUT.Range("A1:B1").Value = [{"Tag","Owned"}]
        NR = 2
        Set tagNEXTFIND = .Range("A:A").Find("tag", tagFIND, LookIn:=xlValues, LookAt:=xlWhole)
        
        Do
            wsOUT.Range("A" & NR).Value = tagFIND.Offset(, 1).Value
            Set tagOWNED = .Range(tagFIND, tagNEXTFIND).Find("owned", LookIn:=xlValues, LookAt:=xlWhole)
            If tagOWNED Is Nothing Then
                wsOUT.Range("B" & NR) = "not found"
            Else
                Range(tagOWNED, tagOWNED.End(xlToRight)).Offset(, 1).Copy wsOUT.Range("B" & NR)
                Set tagOWNED = Nothing
            End If
            
            NR = NR + 1
            Set tagFIND = tagNEXTFIND
            Set tagNEXTFIND = .Range("A:A").Find("tag", tagFIND, LookIn:=xlValues, LookAt:=xlWhole)
        Loop Until tagNEXTFIND.Row < tagFIND.Row
        
        Set tagOWNED = .Range(tagFIND, tagFIND.End(xlDown).End(xlDown)).Find("owned", LookIn:=xlValues, LookAt:=xlWhole)
        wsOUT.Range("A" & NR).Value = tagFIND.Offset(, 1).Value
        If tagOWNED Is Nothing Then
            wsOUT.Range("B" & NR) = "not found"
        Else
            Range(tagOWNED, tagOWNED.End(xlToRight)).Offset(, 1).Copy wsOUT.Range("B" & NR)
        End If
        .Rows(1).Delete xlShiftUp
        Application.ScreenUpdating = True
    End With
    
    End Sub
    How/Where to install the macro:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save as a macro-enabled workbook

    The macro is installed and ready to use. Press Alt-F8 and select CollectTagInfo from the macro list. It will run on the "activesheet", so you can put this macro into another workbook to run from, if you wish.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  15. #15
    Registered User
    Join Date
    10-17-2011
    Location
    Seattle, USA
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Extraction of values following a pattern

    I see that you are on line, so I wanted to tell you that I've succeeded after some sweat to run the macro (I ran through a compile error, I don't know what I did wrong, and don't ask me how I overcome it).
    I think that I will have to "play" with it for a while, to adapt it to my needs (I'm confident in this aspect)
    Once I will be done with my project, then I will start to look more seriously to VBA coding. I want to develop the little seed that you have planted here.

    I hope that I've not been your most time consumer patient

    Thank you again very much for your help, you have by far exceeded my highest expectations.
    Best regards,
    Guillaume

+ 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