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

StackOverflow https://stackoverflow.com/questions/16449076

  •  14-04-2022
  •  | 
  •  

Question

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!

Was it helpful?

Solution 2

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

OTHER TIPS

Yup! You make use of a vlookup.

In spreadsheet 1, in column D row 1, you put the following:

=VLOOKUP(C1,Sheet2!A:B,2,0)

Assuming that the second spreadsheet is named "Sheet2". You can then copy/paste this into the other cells in the spreadsheet or fill it down.

vlookup takes the value in C1 and looks for the index column A in sheet two, and returns the value in the second column (see the '2' I used).

You need to select the required columns, meaning if you're looking in column C of Sheet2, you will need to have =VLOOKUP(C1,Sheet2!A:C,3,0) instead.

The 0 at the end means exact match. You will generally want to use this most of the time. You can use FALSE as well, which means the same thing.

If you use TRUE or 1 instead, you will an approximate match, which you probably don't want right now. You can do some research as to how the approximate match works. There's plenty of documentation on that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top