LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default how to put formula in a cell

Note that a the .Formula property accepts a string. When you write
"=if(val20 < 0,val1... then it's treating val1 as part of the string. The
formula will be looking for named ranges or labels called "val1" and "val20"

Try something like:
ActiveCell.Formula = "=if(" & val20 & " < 0," & val1 & "/(" & val20 &
"/100)," & val1 & "/0.94)"

But more likely you want to reference the address of the cell and not
hardcode the value into the formula:
ActiveCell.Formula = "=if(" & Cells(x, TYCol).Address & " < 0," & Cells(x,
CJCol).Address & "/(" & Cells(x, TYCol).Address & "/100)," & Cells(x,
CJCol).Address & "/0.94)"

Play with the parameters of .Address() to get the absolute/relative options
you desire.


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"monika" wrote in message
...
hello...

I was initially using a recorded macro for putting a formula to range of
cells. now i want it to be converted to a to a proper logic. I am unable

to
put a formula to a cell. Which method or what do i need to use to put
it....Please see my code below:
I tried using Cells(x, VCol).Value...which didn't work. I activated the
particular cell and then used ActiveCell.Formula which also didn't work.
Would really appreciate if anyone could suggest something ...

thanks a lot.

Monika

Sub Macro1()
Dim lastcellnum As Integer
lastcellnum = Cells(Rows.Count, "A").End(xlUp).Row

Dim range1, range2, range As range
Dim VCol, TYCol, CJCol As Integer

Set range1 = Cells.Find("Volume") 'FINDING COLUMN Volume
VCol = range1.Column
Set range2 = Cells.Find("Test_yield") 'FINDING COLUMN Volume
TYCol = range2.Column
Set range3 = Cells.Find("Cum_Jan") 'FINDING COLUMN Volume
CJCol = range3.Column

Dim x As Integer
Dim val20 As Long
Dim val1 As Long

x = 2
With Workbooks("rough.xls").Worksheets("raw_data").Acti vate
While x <= lastcellnum
val20 = Cells(x, TYCol).Value
val1 = Cells(x, CJCol).Value
Cells(x, VCol).Activate
'Cells(x, VCol).Value = "=if(val20 < 0,val1/(val20/100),val1/0.94)"
ActiveCell.Formula = "=if(val20 < 0,val1/(val20/100),val1/0.94)"

x = x + 1
Wend
End With
End Sub




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I make a blank cell in a formula cell with a range of cell Vi Excel Discussion (Misc queries) 5 June 21st 07 02:46 PM
adding a formula in a cell but when cell = 0 cell is blank Mike T Excel Worksheet Functions 5 May 31st 05 01:08 AM
Cell Formula reference to cell Based On third Cell Content Gabriel Excel Discussion (Misc queries) 1 February 11th 05 06:36 AM
Cell Formula reference to cell Based On third Cell Content Gabriel Excel Discussion (Misc queries) 0 February 11th 05 05:35 AM
Question: Cell formula or macro to write result of one cell to another cell Frederik Romanov Excel Programming 1 July 8th 03 03:03 PM


All times are GMT +1. The time now is 07:41 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"