0

sorry for being a total noob in excel!

I have two sheets, sheet 1 named "Stocks" and sheet 2 named "Stocks search". In "Stocks" I have from A1 to B700 values. In A column I have the stocks symbols and in B column I have the stocks' issuers symbols, so every entry in A column is unique, yet there can be repeating entries in column B.

So in sheet "Stocks search", if I enter in A1 an issuer's symbol, I want for the formula to go search in sheet "Stocks" and fetch all stocks that this issuer has in new rows.

How can this be done in a formula? Thanks in advance!

  • From you public profile, we can see you haven't taken the [Tour](https://stackoverflow.com/tour) yet, I suggest you do so. That said, it looks like you need to use `INDEX/MATCH`. Since you need to return multiple values, [this](https://stackoverflow.com/questions/21288782/excel-index-match-multiple-row-result) may get you started – cybernetic.nomad Sep 27 '18 at 14:42
  • Hello and welcome to the site. Just a heads up, it is common for experienced members to be rude and condescending with their answers when they believe the answer is trivial. They mean no disrespect but are just very to the point. I apologize for this. The website can offer many useful answers otherwise. Back to your question. The best answer for you is as nomad mention, 'INDEX'. I will comment below in a moment once i have an exact formula. – jallington Sep 27 '18 at 15:37
  • 1
    Excel? Or Google Sheets? If only one... Then why both tags? – tehhowch Sep 28 '18 at 13:21

2 Answers2

1

This is a VBA solution to the question. IMHO, this is more appropriate than a formula (in this case). The formula approach is OK, but there are drawbacks - you have to remember the CSE rule, and then copy the formulas down the right number of rows (which you don't know in advance), etc, etc.

This code uses the same assumptions as the formula approach.

1 - sheets = Stocks and Stock report
2 - Data in Sheets, columns A and B (header in row 1)
3 - lookup code is on Stock report
4 - Output is on Stock report

One of the advantages is that if new data is added to the Stocks sheet (i.e. the bottom row > 700), the vba automatically adjusts.

The code is self-documented. But the essence is that it creates an autofilter on "Stocks" using the lookup value as the criterion; copies the rows that meet the criteria; and pastes the result to an output range on "Stock reports". The output range is cleared before the copy/paste takes place so that there are no left-overs from any previous lookup.

I think there's something to be said for creating a dropdown list for the lookup cell. No doubt that could be automated too by getting the codes from Column A, getting the unique values, and then apply them to the lookup cell. Just a thought;)


Sub so_52537740()

    ' CREDITS
    ' 
    ' Refer: https://stackoverflow.com/questions/17531128/copy-paste-calculate-visible-cells-from-one-column-of-a-filtered-table
    ' Date: 8 July 2013
    ' Submitted by: Jon Crowell (https://stackoverflow.com/users/138938/jon-crowell)


    Dim src As Worksheet, tgt As Worksheet
    Dim filterRange As Range, copyRange As Range
    Dim lastRow As Long
    Dim stocks As String, stockreport As String

    ' set values for sheet names
    stocks = "Stocks"
    stockreport = "Stock report"

    ' set values for Sheet variables
    Set src = ThisWorkbook.Sheets(stocks)
    Set tgt = ThisWorkbook.Sheets(stockreport)

    ' clear the exist target data
    tgt.Range("A4:B" & Rows.Count).ClearContents

    ' turn off any autofilters that are already set
    If src.AutoFilterMode Then src.AutoFilter.ShowAllData

    ' find the last row in the Stocks sheet with data in column A
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row

    ' the range that we are auto-filtering (all columns)
    Set filterRange = src.Range("A1:B" & lastRow)

    ' the range we want to copy (only columns we want to copy)
    ' in this case we are copying both columns A and B
    ' we set the range to start in row 2 to prevent copying the header
    Set copyRange = src.Range("A2:B" & lastRow)

    ' filter range based on column A being equal the the value in Cell A1 of the stockreport
    ' consider making this a dropdown list so that there are no errors
    filterRange.AutoFilter field:=1, Criteria1:=Format(Sheets(stockreport).Range("a1").Value)

    ' copy the visible cells to our target range
    ' note that you can easily find the last populated row on this sheet
    ' if you don't want to over-write your previous results
    copyRange.SpecialCells(xlCellTypeVisible).copy tgt.Range("A4")

    ' turn off any autofilters that are already set
    If src.AutoFilterMode Then src.AutoFilter.ShowAllData

End Sub

Giving due credit: There is, as they say, nothing new under the sun. I have based this answer on an excellent piece of work by Jon Crowell on a question in StackOverflow "Copy/Paste/Calculate Visible Cells from One Column of a Filtered Table" in July 2013. Just goes to show what a bit of Googling and perseverance can achieve.

Tedinoz
  • 5,911
  • 3
  • 25
  • 35
0

I believe I have an answer for you.

Try

=IFERROR(INDEX('Stocks Search'!$A$1:$A$700,SMALL(IF('Stocks Search'!$B$1:$B$700=$A$1,ROW('Stocks Search'!$A$1:$A$700)-MIN(ROW('Stocks Search'!$A$1:$A$700))+1),COLUMNS($A$1:A1))),"")

This is a CSE formula. What that means is once you enter it into cell B1, you will need to press Control+Shift+Enter. Once you do this, these brackets will appear around your formula {}

Click the fill button in the bottom right of the cell and drag the formula to the right (you will need to do this for as many cells as it is possible for answers). So if Company A has 40 possible answers, you will need to have this formula go at least 40 cells to the right.

The application of CSE formulas can be tricky. Essentially you need to go to the end of the formula in the formula bar, and then use Control+Shift+Enter.

I hope this helps.

jallington
  • 181
  • 2
  • 13
  • @nataliikaa, please remember to accept my answer with the check-mark if this was the correct answer :) – jallington Sep 27 '18 at 18:17
  • There's an error in your code-it doesn't seem to work. In addition, the sheet with the data is "Stocks", the report is on "Stocks search". FWIW, I think it makes more sense to copy down the page than across (but that's just my opinion).This formula goes in cell A2 on "Stocks_search". As jallington says, its requires CSE to work. Then just copy down as many rows as necessary..`'=IF(ISERROR(INDEX(Stocks!$A$1:$B$700,SMALL(IF(Stocks!$A$1:$A$700=$A$1,ROW(Stocks!$A$1:$A$700)),ROW(1:1)),2)),"",INDEX(Stocks!$A$1:$B$700,SMALL(IF(Stocks!$A$1:$A$700=$A$1,ROW(Stocks!$A$1:$A$700)),ROW(Stocks!1:1)),2))` – Tedinoz Sep 28 '18 at 03:45
  • I mean to add that a far more simple solution would be to be to filter the two columns, and then select the criteria in Column A and read off the results in Column B. – Tedinoz Sep 28 '18 at 03:51