Search code examples
excelformulas

Excel - If cell contains string or numbers, insert another string next to it?


I'm wondering if it's possible to do this in excel:

Let's say I have columns A, B, and C.

Column C contains numbers, for example, 234 or 590

I want "Apple" added to column D if 234 appears in column C in the cell to the left of it. I want "Orange" added to column D if 590 appears in column C in the cell to the left of it.

If this is possible... I'm wondering if I can take it one step further:

Let's say in another tab in the same spreadsheet, I have a column with a number and a column with a string corresponding to each number (ex: 234 = apple, 590 = orange, 300 = pear). Is there a way for me to tell excel, "if the number in a cell in one spreadsheet matches the number in a cell in another spreadsheet, insert the string next to the cell in the other spreadsheet into the current spreadsheet next to the cell containing that same number."

That might sound confusing... so here is an example:

Spreadsheet 1 has 3 columns formatted like this:

3/5  | Apple  | 500
3/7  | Pear   | 200
3/9  | Banana | 100

Spreadsheet 2 has the following columns:

500 | Super Cool
250 | Very Cool
200 | Cool
150 | A little cool
100 | Warm

I want to add a column in spreadsheet 1 next to each number containing the corresponding string. So it becomes:

3/5  | Apple  | 500 | Super Cool
3/7  | Pear   | 200 | Cool
3/9  | Banana | 100 | Warm

Does this make sense? Let me know if you need any clarification and if anyone could help me out.

Thanks!


Solution

  • You don't need VBA for this. The simple Lookup formula will work. Use the Formula Wizard - click Formulas, then Lookup and Reference, and follow the prompts to provide input.

    For instance, given the following data:

        A         B
    1  Pear      500
    2  Apple     700
    3  Banana    600
    

    And the following lookup table:

         J        K
    1   500      Cool
    2   600      Very cool
    3   700      Super cool
    

    Insert the following formula into cell C1

    =Lookup(B1, J1:J3, K1:K3)
    

    and then copy it into C2 and C3

        A         B       C
    1  Pear      500     Cool
    2  Apple     700     Super cool
    3  Banana    600     Very cool