# If a string is contained in a row return the value of the first cell of the row

I would like to:

• check the row in Sheet1 where the domain in Sheet2 is contained
• return in Sheet2 the related values from the field USER and IP from that row in Sheet1

For example `Sheet2!B3` and `Sheet2!B4` should return `user1` and `pass1`. How to do this?

• Please try to make your question self-contained. Linking to a sample spreadsheet is great to supplement the information, but it would be better if all the information needed was in your question. That link will eventually stop working or, as in my case, Google Drive may not be reachable.
– ale
May 27, 2016 at 15:50
• Your link goes to a different sheet. Feb 22, 2017 at 18:26

The Arrayformula can keep you from copying the formula down. For your use, you want to use vlookup to find and return the value. You will only be able to return the values for one matched domain, so I will assume this is coming from column H in your images.

The simple version places a formula in each column, starting with the USERNAME, place this in cell B2 and make sure cells B3 to the last cell in the column are empty:

``````=ARRAYFORMULA( IF(ISBLANK(H2:H),,VLOOKUP(H2:H,Sheet2!A2:C,3,FALSE)))
``````

This breaks down as follows. The `IF(ISBLANK(H2:H),,` takes care of empty rows, returning no value if cell H in a given row. Note that this is very different from returning "" such as `IF(ISBLANK(H2:H),"",` which in Google Sheets actually returns a value of nothing. The VLOOKUP() looks in Sheet2, columns A through C for a value from column H. If one is found it returns the value from the third column. You could do a similar function in cell C2, making a couple minor changes.

If you want to get REALLY tricky, you can combine both of these and place the formlua in cell B1, making header entries and populating the columns with one formula:

``````=ARRAYFORMULA(IF(ROW(A1:A) = 1, {"USERNAME", "PASSWORD"}, IF(ISBLANK(H1:H),, {VLOOKUP(H1:H, Sheet2!A2:C, 3), FALSE), VLOOKUP(H1:H, Sheet2!A2:D, 4, FALSE)})))
``````

This first checks to see if the arrayformula is working in row 1. If it is, it returns an array of the headers via `{"USERNAME", "PASSWORD"}` The curly brackets define an array. The second set of curly brackets returns an array of the two VLOOKUP formulas.

Make sure there is NOTHING else in any of the cells in columns B or C.

One approach is to put the following in cell Sheet2!B2, dragging it down the column.

``````=iferror(indirect("Sheet1!A" & min(arrayformula(if(Sheet1!A\$2:J = A2, row(Sheet1!A\$2:J), )))), "no match")
``````

It will return the first entry in Sheet1's column A for which the row contains a value in Sheet2!A2. If no match, it returns "no match".

Same can be done for User column, by replacing the first argument of `indirect` by "Sheet1!B".

Explanation: arrayformula transforms the array of values Sheet1!A\$2:J by replacing each entry with the row number, if it matches the given string, or leaves blank if it doesn't.

Then `min` finds the minimum of all these values. The result is the row number, which can be used in `indirect` command to retrieve any desired entry from that row. If there is no match, the minimum of an empty range will be 0, which throws an error (there is no cell A0). The error is intercepted by `iferror`, which displays a user-friendly message.

• if I try to put in Sheet2!B2 it gives me an error. Thank you May 28, 2016 at 10:08