gaffcz
gaffcz

Reputation: 3479

Add formula containing condition to cell

I'm trying to add following formula:

=IF(A1="string_condition";"variant1";"variant2")

to selected range of cells (B1:B10) using VBA this way:

Range("B1:B10").Formula = "=IF(A1=""string_condition"";""variant1"";""variant2"")"

but it doesn't work, it reports following message:

Runtime error 1004
Application-defined or object-defined error

where is the problem? this testing formula (without strings) works normally:

Range("B1:B10").Formula = "=SUM(C1:C10)"

Upvotes: 1

Views: 82

Answers (1)

user2140173
user2140173

Reputation:

In VBA you don't use semi colons you use commas

Sub Main()
 Range("B1:B10").Formula = "=IF(A1=""yes"",""no"",""yes"")"
End Sub

Note: you wrap double quotes with an extra quote inside a string to escape it.

Upvotes: 1

Related Questions