Double quotes is the key.
Sub InsertIF()
ActiveSheet.Range("Q2").Value = "=if(M2=1,""X"",if(M2=2,""Y"",""Z""))"
End Sub
Alternatively you can use the FormulaR1C1 syntax:
ActiveCell.FormulaR1C1 = "=IF(RC[-4]=1,""X"",IF(RC[-4]=2,""Y"",""Z""))"
Or you can write the If statement into the VB code:
Sub IfVBA()
If ActiveSheet.Range("M2").Value = "1" Then
ActiveSheet.Range("Q2").Value = "X"
ElseIf ActiveSheet.Range("M2").Value = "2" Then
ActiveSheet.Range("Q2").Value = "Y"
Else
ActiveSheet.Range("Q2").Value = "Z"
End If
End Sub
Bookmarks