Forum Discussion

FatManFluff's avatar
FatManFluff
Brass Contributor
Nov 09, 2024
Solved

VBA to add IF Statement

Good morning all,

Having a hard time getting VBA to set a formula into a cell when I use the reset button to delete all contents. Currently I am using:

 

Sub AddFormula()

Range("P4").Value = "IF(D4<>"",D4,"")""

End Sub

 

Now I'm assuming it's not working because you have to add everyone inside the " " but since there is "" to determine if cell is empty, it is throwing it off. I'm fairly new to VBA so I know it's probably an easy fix just can't figure it out.

 

PS: The reason I want to add an IF formula into the sheet instead of VBA doing it for me is because I have people using the sheet on an iPad and macros isn't support on the iPad (unless there is work around I'm not aware of) 

  • Try this:

    Sub AddFormula()
        Range("P4").Formula= "=IF(D4<>"""",D4,"""")"
    End Sub

     

3 Replies

  • Try this:

    Sub AddFormula()
        Range("P4").Formula= "=IF(D4<>"""",D4,"""")"
    End Sub

     

    • FatManFluff's avatar
      FatManFluff
      Brass Contributor

      Any clue why when I add more than just that 1 range it throws off the formula? EX when I do a second range P5 the formula that gets added is

       

      =@ID(D5<>"",D5,"")

       

      The sheet I'm using has 2 sides an income and outcome and I reference

      C to O

      D to P

      E to Q

      I want to do the same formula for P4:Q33 if there is an easier way instead of adding 1 line per cell that would be very helpful.

    • FatManFluff's avatar
      FatManFluff
      Brass Contributor

      Perfect this worked!! I was so close yet so far! Appreciate the quick reply have a good weekend

Resources