+ Reply to Thread
Results 1 to 6 of 6

search array of cells in one workbook and return the value of the adjacent cell

  1. #1
    Registered User
    Join Date
    06-04-2013
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    3

    search array of cells in one workbook and return the value of the adjacent cell

    I am looking to search a 2 column pivot table located in a different workbook. Column A contains text and Column B contains a figure associated with the text. I would like to search for a text string and if the search finds it, to return the value in the adjacent cell into the workbook where the macro was assigned to. I dont really know where to begin with this but some sort of VBA code would be appreciated and i'll be happy to play from there.

    The text column in the pivot table is something along the lines of Loop 00, Loop 01, Loop 02 etc etc and the associated figures could be any numerical value.

    Thanks in advance!

  2. #2
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: search array of cells in one workbook and return the value of the adjacent cell

    Hi

    I'm not sure that you need VBA to achieve what you want.
    Are you aware of the GetPivotData function?

    Take a look at the attached example, where I have tried to create on a single sheet something similar to your description.
    There is a table of data in columns A:B which are then summarized with a simple Pivot Table, located at F3
    In column J:K is a report, which contains just some of the headings that appear in the PT.

    Using the GetPivotData function, if you were to place your cursor in cell K4 and type an = and then point to the value you want to extract from the PT, say cell G2, then you would get
    =GETPIVOTDATA("Value",$F$3,"Type","Loop2")

    If you now change that formula, so that the Type is not referring to "Loop2", but instead refers to the heading that is in J4, you would have
    =GETPIVOTDATA("Value",$F$3,"type",$J4)

    This can then be copied down column K of your report, and it will extract the PT values that match the headings that have entered in column J.
    Attached Files Attached Files
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  3. #3
    Registered User
    Join Date
    06-04-2013
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: search array of cells in one workbook and return the value of the adjacent cell

    thanks for that, it works perfctly and I've got it locating a pivot table in a different workbook too.

    One more thing I'd like to do is to have it give me the SUM of two adjacent values. For instance, in the example you gave me, id like to find the SUM of Loop1 + Loop2. Is this possible?

    Again, I really appreciate your help on this. Its saved me a lot of

  4. #4
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: search array of cells in one workbook and return the value of the adjacent cell

    Hi

    Well you can do it directly from the PT by using GetPivotadata twice

    =GETPIVOTDATA("Value",$F$3,"Type","Loop1")+GETPIVOTDATA("Value",$F$3,"Type","Loop2")

    Alternatively, you could pick it up from your final Results Table with Index and Match
    =SUM(INDEX(K:K,MATCH($N$3,J:J,0)):INDEX(K:K,MATCH($O$3,J:J,0)))
    where N3 holds the first value e.g. Loop1 and O3 holds the second value - Loop2

    I have shown an example on the attached workbook
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-04-2013
    Location
    singapore
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: search array of cells in one workbook and return the value of the adjacent cell

    That step is complete and working, I did it using the GETPIVOTDATA function. Thanks....

    Now, I've come to realise that the Loops I need to add may not always be present. For example, I will need to return a value which is the sum of Loop 00, Loop 01 and Loop 02. However, on some days Loop 00 may not be in the data. Clearly as a function this is where it will fall down as it will just give me an N/A return. Is there a way to nest some sort of IF function which does a search or covers an array of data to return the values which are present?

    Again, thanks in advance! I'll stop asking for help, promise!

    T

  6. #6
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: search array of cells in one workbook and return the value of the adjacent cell

    Hi

    Take a look at the attached workbook, maybe it is more like your data as I have added a Month column to the source, and I have some months with no data for the loops.

    Taking Months to the Report Filter area and selecting an individual month, the standard layout of the Pt would just show the Loops that have data in that month.
    However, if you right click on the PT and select Field Settings, and then the Layout & Print tab, you will see an option to show Items with No data.
    Select this, and then your summation will work, using either multiple GetPivotData functions, or using the Index method that I showed previously.

    If this doesn't solve your problem, perhaps you could upload a sample of your data, PT and report (all on a single sheet as I have done, rather than in separate workbooks)
    Attached Files Attached Files

+ 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