How can a formula be capture in a variable?

MPW

Well-known Member
Joined
Oct 7, 2009
Messages
571
Office Version
  1. 365
Platform
  1. Windows
Hey,

I need to trap the formula instead of the value of a cell into a variable.
I was trying something like this.
PHP:
Dim cf, cv
If Range("A1").HasFormula = True Then
    cf = Range("A1").Formula
ElseIf Range("A1").HasFormula = False Then
    cv = Range("A1").Value
End If
Ideas?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Dim cf as String
cf = Range("A1").Formula

If A1 contains a formula, this will be held in cf (as a string). If A1 contains a constant or text, this will be held in cf.
 
Upvote 0
Code:
Sub getForm()
Dim cf, cv
If Range("A1").Formula Like "=*" Then
    cf = Range("A1").Formula
Else
    cv = Range("A1").Value
End If
End Sub
 
Upvote 0
Sorry about that, I left that out of the Dim Statement.
It didn't help though.

I agree that it should work.
It gives me a runtime error.
 
Upvote 0
The HasFormula = True statement actually works pretty good.

I do think I found my problem.

I was reproducing the formula from one workbook to another. The original workbook was protected. I didn't think it would matter since I wasn't changing it but apparently I was wrong.

Thanks for taking the time.
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,461
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top