View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Garich Garich is offline
external usenet poster
 
Posts: 2
Default Formula based on another cell value



"Fester" wrote:

I want to run a loop that looks at a value in one cell, and then
enters one formula for one value, and another for a different value.

So if Cell B2="X" then
range("E2").value = "formula"
elseif Cell B2 = "Y" then
range("E2").value = "other formula

this would loop through all cells until it found an empty cell.

Any help is appreciated.

Brendon




Sub QuickSample()


Dim row


'==== version 1 ==========

For row = 1 To Cells.SpecialCells(xlCellTypeLastCell).row
Cells(row, 2).Select
If Cells(row, 2) = "" Then
row = 9999
col = 9999
ElseIf UCase$(Cells(row, 2)) = "X" Then
Cells(row, 5) = "formula"
ElseIf UCase$(Cells(row, 2)) = "Y" Then
Cells(row, 5) = "other formula"
End If
Next row


'==== version 2 ==========


For row = 1 To Cells.SpecialCells(xlCellTypeLastCell).row
If Cells(row, 2) = "" Then
row = 9999
col = 9999
ElseIf UCase$(Cells(row, 2)) = "X" Then
Cells(row, 5) = "=C" + Trim$(Str$(row)) + "*2 "
ElseIf UCase$(Cells(row, 2)) = "Y" Then
Cells(row, 5) = "=sum(C4:C" + Trim$(Str$(row)) + ")"
End If
Next row



End Sub