Making VBA create a LOOKUP formula in a worksheet

erik11

New Member
Joined
Apr 30, 2011
Messages
11
Dear MrExcel users

My task is to create some VBA code, which after execution will place a certain formula inside a cell. You can think of the formula in terms of an exam: The formula takes as input the score of a student and gives as output the corresponding grade. If the score is less than 10, the output should be an "F", if the score is at least 10 but less than 20, the output should be an "E", ... , if the input is at least 50, the output should be an "A". So if the score is for example 32, the student should get a "C".

I can pretty easily write some VBA code which produce a formula doing the job, if I use nested IF commands. It will however be a pretty long formula and it will be hard to overview, so I am looking for something better. Then I stumpled upon the LOOKUP command. I actually made it work when I tested the formula manually in a worksheet writing the formula, assuming the input number is in cell A1:

=LOOKUP(A1,{0,10,20,30,40,50,100},{"F","E","D","C","B","A"})

NB! I hope I translated it correctly. I use a Danish version of Excel. This sometimes creates a little pain, since in the worksheets Danish words are being used and comma as decimal separator instead of a dot, and semicolon instead of a comma to separate arguments inside formulas. On the other hand inside the Excel VBA editor everything is american :)

Now I would like to know if this LOOKUP command is the right way to go and if so, how do I write the VBA code to produce the formula in say cell B1? I have tried doing it, but I have troubles getting it to work. The syntax I have tried is not accepted. I was not able to use the { parenteses for example ...

I hope someone can help!

Regards, Erik
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this (keep the , as Excel should substitute ; automatically)

Code:
Sub a()
Range("B1").Formula = "=LOOKUP(A1,{0,10,20,30,40,50,100},{""F"",""E"",""D"",""C"",""B"",""A""})"
End Sub
 
Upvote 0
Thanks a lot, Peter. It worked!

By by the way: Somewhere it is mentioned that one should use the command HLOOKUP instead. What is the reason for this?

Regards, Erik
 
Upvote 0
You are probably right, that HLOOKUP and VLOOKUP don't really apply here. Maybe they are better if the target is to be compared to the content of a whole range of cells in a worksheet?

Anyway my problem is solved. Again big thanks!

Erik
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

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