Change Value of cell based on value in another cell (many possible values)

acseric

New Member
Joined
Sep 20, 2016
Messages
46
I am trying to set values in all of the cells in a given column, depending on the value of another column.

In this case Column AJ has 2 character state abbreviations. I want to set Column C a different hard coded value based on values in AJ

So, if column AJ contains (AK, AZ, AR, CA, CO, ID, IA, KS, LA, MN, MO, or MT) set C = "Joe"
or if column AJ contains (NE, NV, NM, ND, OK, OR, SD, TX, UT, WA, or WY) set C = "Sally"
or if column AJ contains (AL, CT, DE, FL, GA, IN, IL, KY, ME, MD, or MA) set C = "Pete"

Is there a better way than creating a giant nested IF?

Any help is greatly appreciated.

Eric
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I'd create a 2-column table with the state IDs and the names in it, like:

AB
1AKJoe
2ARJoe
3AZJoe
4NESally
5ALPete

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet6



etc.

Then in column C, just use a VLOOKUP:

=VLOOKUP(AJ2,$A$1:$B$50,2,FALSE)
 
Upvote 0
Is there a way to do this with VBA?

I would like to create a macro so I can reuse it. Also, can the lookup table be on Sheet2, and have the macro look up the values based on column A and B on the second sheet?

Thanks!

Eric
 
Upvote 0
First, you can use the VLOOKUP with a range on another sheet:

=VLOOKUP(AJ2,Sheet2!$A$1:$B$50,2,FALSE)

Second, you can do this with VBA, but there's no reason to. One way is to write a UDF (User-defined function), which would just call WorksheetFunction.Vlookup, so all you're doing is adding overhead to get the same thing done. Another way would be to write a macro that does all of column C/AJ at one time, reading down column AJ, getting the value, looking up the value from Sheet2, then inserting in it column C, then continuing down column AJ until we find an empty cell. But you'd have to initiate it with a hot key, or the macro selector, or a button, or an event handler. Either more hassle, or more overhead.

I'm a big fan of VBA, but when you can get your work done with basic built-in functions, that's usually the way to go.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,860
Members
449,194
Latest member
HellScout

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top