View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default Using code to insert different formulas

Use Select case Statement as below.. Checkout the below link for help
http://www.vbtutor.net/lesson8.html..

The below will check the value of colA if text "A" exits assign cell B and C
with formula

Sub Macro()
Dim cell As Range
For Each cell In Range("A1:A16")
Select Case cell.Value
Case "A"
cell.Offset(, 1).FormulaR1C1 = "=RC[2]*RC[3]"
cell.Offset(, 2).FormulaR1C1 = "=RC[3]*RC[4]/RC[5]"
Case "B"
'formula to be altered to suit
'cell.Offset(, 1).FormulaR1C1 = "=RC[2]*RC[3]"
'cell.Offset(, 2).FormulaR1C1 = "=RC[3]*RC[4]/RC[5]"
Case "C"
'formula to be altered to suit
'cell.Offset(, 1).FormulaR1C1 = "=RC[2]*RC[3]"
'cell.Offset(, 2).FormulaR1C1 = "=RC[3]*RC[4]/RC[5]"
End Select
Next
End Sub


--
Jacob


"Roger on Excel" wrote:

[Excel 2003]

I have sixteen different conditions which can exist. These relate to
whether certain aspects are TRUE or FALSE. When taken together I have 16
unique "cases".

These appear in cells A1:A60 and are labeled Condition1, 2, 3 etc...
Condition16

Depending on the condition in each cell in column A, I need certain formulas
to operate in adjacent cells in colomn B and C

For example for Condition1 in A1, I need the formula in B1 to be "=D1*E1"
and the formula in C1 to be "F1*G1/H1"

Each formula referes to cells in the same row, so perhaps some loop could be
used to read down Column A1:A60 and populate the adjacent cells in Columns
B1:B60 and C1:C60 with the appropriate formula, depending on the Condition or
"case"

I know from experience that one can enter formulas into specific cells using
the following type of code

Range("B1").Select
ActiveCell.Formula = "=D1*E1"

So presumably one can create code to check Column A "Conditions" and insert
the appropriate formulas?

Can anyone help?

Thankyou,

Roger