ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Insert a Formula with a Macro (https://www.excelbanter.com/excel-discussion-misc-queries/115503-insert-formula-macro.html)

Ed

Insert a Formula with a Macro
 
Hello I have the following macro but I would like to rather than insert the
value to insert a IF formula:

Dim LastRow As Long
LastRow = Worksheets("Sheet A").Range("O2")
Sheets("Registry").Select
Range("B" & LastRow) = "=Row(B" & LastRow - 1 & ")"
Range("C" & LastRow) = Worksheets("Sheet A").Range("K2")
Range("D" & LastRow) = Worksheets("Sheet A").Range("K12")

For the 2 last lines, I would like that instead of inserting values K2 and
K12, I would like to insert a IF formula on Sheet "Registry"

=IF(A&LastRow="X","-",Worksheets("Sheet A").Range("K2"))

substituting with the values would give things like in the cell.

(LastRow = 6)

For C6 =IF(A6="X","-","13500")
For D6 =IF(A6="X","-","21/Oct/06")

and so on...

thanks!


kassie

Insert a Formula with a Macro
 
To insert a formula, start with Range("your range").Formula =

Enclose the formula in "". Should you use quotation marks (") in the
formula, eg to say ="", then remember to enclose these marks in quotation
marks as well, eg """". If you use variables to generate the formula, eg for
lastrow, then put a quotation mark (") and an ampersand ("&") before the
variable, and an ampersand and quotation marks after the variable, eg
Range("A1").Formula = "=IF(D" & vLastrow & "="""","""")"

"Ed" wrote:

Hello I have the following macro but I would like to rather than insert the
value to insert a IF formula:

Dim LastRow As Long
LastRow = Worksheets("Sheet A").Range("O2")
Sheets("Registry").Select
Range("B" & LastRow) = "=Row(B" & LastRow - 1 & ")"
Range("C" & LastRow) = Worksheets("Sheet A").Range("K2")
Range("D" & LastRow) = Worksheets("Sheet A").Range("K12")

For the 2 last lines, I would like that instead of inserting values K2 and
K12, I would like to insert a IF formula on Sheet "Registry"

=IF(A&LastRow="X","-",Worksheets("Sheet A").Range("K2"))

substituting with the values would give things like in the cell.

(LastRow = 6)

For C6 =IF(A6="X","-","13500")
For D6 =IF(A6="X","-","21/Oct/06")

and so on...

thanks!


Ed

Insert a Formula with a Macro
 
Hello kassie, thanks for your help

but well... I didn't get this thing to work... I have some problems with the
"" and the parenthesis, I will give it a harder try on monday...

thanks again


All times are GMT +1. The time now is 01:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com