Can I use an IsFormula function in a VBA routine?

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
402
Office Version
  1. 2007
Platform
  1. Windows
Hello,

I recently responded to an old 2002 post regarding creating an isFormula function for Excel 2007.
How to test if a specified cell contains a formula? (see Response #33).

The function (which works great) is:
VBA Code:
Function IsFormula(cell_ref As Range)
     Application.Volatile
     IsFormula = cell_ref.HasFormula
End Function

My question : Is there a way to include this function in a VBA IF statement? Something like

Code:
If IsFormula(B33) = True Then
    code to do THIS
Else
    code to do THAT
End If

Thanks,
Steve K.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
@EssKayKay
Yes, like below.

VBA Code:
If Application.WorksheetFunction.IsFormula(Range("I24")) Then
    MsgBox "Yes there is a formula in range " & Range("I24").Address
Else
    MsgBox "No there is not!"
End If

But why would you not avoid calling the function and use the direct via query, below?

VBA Code:
If Range("I24").HasFormula Then
    MsgBox "Yes there is a formula in range " & Range("I24").Address
Else
    MsgBox "No there is not!"
End If
Hope that helps.
 
Upvote 0
Solution
@EssKayKay
Yes, like below.

VBA Code:
If Application.WorksheetFunction.IsFormula(Range("I24")) Then
    MsgBox "Yes there is a formula in range " & Range("I24").Address
Else
    MsgBox "No there is not!"
End If

But why would you not avoid calling the function and use the direct via query, below?

VBA Code:
If Range("I24").HasFormula Then
    MsgBox "Yes there is a formula in range " & Range("I24").Address
Else
    MsgBox "No there is not!"
End If
Hope that helps.

Thank you Snakehips for your quick response. I tried your first suggestion. However it returned:
Run-time error ‘438’ Object doesn’t support this property or method.

So I tried your second HasFormula suggestion – it worked perfectly. I have a few things to address yet but we’re going in the right direction.

Again thanks and much appreciated.
Steve
 
Upvote 0
As @Fluff pointed out I completely overlooked the fact that first suggestion was calling the ISFORMULA function native to 365 rather than your IsFormula function.
Apologies for that but pleased that the second approach is working for you..
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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