Search code examples
excelvbaexcel-formulacell

Range().value="=(if) ... " in Excel


I'm a beginner. Can you please help me with the below code?

Range ("B2","B16").value="=if(B1="","",if(B4=B5,"Completed",""))

It doesn't work.


Solution

  • Its really not clear what the intended formula is, but this would be valid if you are trying to put the formula in B2 and B16:

    Union(Range("B2"),Range("B16")).formula = "=IF(B1="""","""",IF(B4=B5,""Completed"",""""))"
    

    If you are trying to put the formula in B2:B16 then:

    Range("B2:B16").formula = "=IF(B1="""","""",IF(B4=B5,""Completed"",""""))"