In this article we will learn how to insert the formula in VBA and what is the use of it to write the VBA code.
When you having problem with inserting formula into cells in VBA. How we should explain the syntax in VBA coding.
For Example: - I want to insert the formula in Cell C2, =B2-A2. Follow below given steps to write the code in VBE.
• Open Excel file and then Press the key Alt+F11 on the keyboard.
• VBE page will get open.
• Write the below mentioned code:-
Sub Add_formula()
Dim A As Range
Dim B As Range
Dim C As Range
Set A = Range("A2")
Set B = Range("B2")
Set C = Range("C2")
C.Value = B.Value - A.Value
End Sub
• To run the Macro press the key F5 on your keyboard.
• Formula will calculate by subtracting the numbers.
By using this way we can easily define the syntax and use of it while writing the VBA code.
Bookmarks