View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.misc
JulianP JulianP is offline
external usenet poster
 
Posts: 3
Default Multiple cell formulas in different cells

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

"Gary''s Student" wrote:

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


--
Gary''s Student - gsnu200859


"JulianP" wrote:

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