Multiple cell formulas in different cells

J

JulianP

I would like to use a pull down menu to control the formulas in different
cells. For example if the pulldown is set to A then the formula in cells
are one thing. If set to B then the formula in that cell would change to
something else. I have though of using if statements, but this gets messy.
Do I need to get in to macro writing? I am looking for something simple.
Cheers
 
G

Gary''s Student

Say we have a Data Validation drop-down in A1 that selects from:
a,b,c,d
and we want to make the formula in A4:

=A1+A2
or
=A1-A2
or
=A1*A2
or
=A1/A2

Insert the following macro in the worksheet code area:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim a1 As Range, t As Range, v As String
Dim formu As String
Set a1 = Range("A1")
Set t = Target
If Intersect(a1, t) Is Nothing Then Exit Sub
v = t.Value
If v = "a" Then formu = "=A2+A3"
If v = "b" Then formu = "=A2-A3"
If v = "c" Then formu = "=A2*A3"
If v = "d" Then formu = "=A2/A3"
Application.EnableEvents = False
Range("A4").Formula = formu
Application.EnableEvents = True
End Sub


Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 
S

Stefi

If number of options is limited, why not simply =IF(A1="X",formula1,formula2)
or =IF(A1="X",formula1,IF(A2="Y",formula2, ...)) etc.

Regards,
Stefi

„Gary''s Student†ezt írta:
 
G

Gary''s Student

The OP specifically asked for an alternative to IF statements.

I agree that your suggestion is the better solution.

Even if the number of options was very large, it would be better to store
all the formulas in a table and then use VLOOKUP()
 
S

Stefi

Yes, I didn't notice that he wants a pulldown of formulae! Maybe he doesn't
know other techniques like embedded IFs, VLOOKUP, etc.

Regards,
Stefi

„Gary''s Student†ezt írta:
 
J

JulianP

Thank you. This works for me because I have different formulars for several
cells that depend on the selected option. I will give it a shot. Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top