MrExcel Publishing
Your One Stop for Excel Tips & Solutions

formula to VBA

Posted by Fons Kreukels on January 27, 2002 4:18 AM

Is there a simple way to get a formula (in a worksheet cell) into VBA code ?

Posted by DK on January 27, 2002 4:38 AM

Yes, there is:-

Sub GetFormula()
Dim strFormula As String

strFormula = ActiveCell.Formula

MsgBox strFormula
End Sub

Hope this helps,

Posted by Micke on January 27, 2002 5:00 AM

If you mean to use the ordinary cellfunctions in a macro. Yes , you can do that.

short example:

Sub test()
' this example gives you the sum of 1 & 2
MsgBox Application.WorksheetFunction.Sum(1, 2)
End Sub

Posted by Montjoy on January 27, 2002 5:19 PM

The easiest way to get the VBA code for the formula you want is to use the macro recorder.