Whether a Cell contains a Formula

G

Guest

What function can be used to determine whether a cell contains a formula?

Thanks in advance for your help.

Roger
 
G

Guest

Try this UDF:
Function IsFormula(cellref)
IsFormula = Left(Range(cellref).Formula, 1) = "="
End Function

Regards,
Stefi


„Roger†ezt írta:
 
B

Bob Phillips

Stefi,

cellref.HasFormula

built-in property of a range

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Bob:

Thanks for your input. I tried Stefi's suggestion, but somehow I can't get
it to work. It may be because I don't know much about macros.

Basically, I have two columns containing formulas. Any of the two columns
can be overwritten with data. In one of the two columns which was not
overwritten with data, I would like it to be changed to write-protected. In
the third column, I want to know which of the first two columns still has the
formula.

How to achieve this? Thanks again in advance.

Regards,
Roger
 
B

Bob Phillips

Add the UDF to a code module and then in the worksheet use

=IF(IsFormula(M1),"M",IF(IsFormula(N1),"N",""))

where I assume M and N are those two columns.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)
 
G

Guest

Bob:

I understand the part you have mentioned. I am having some problem with
Stefi's below-mentioned UDF. Can you please write the UDF again? Thanks.

Regards,
Roger
 
D

Dave Peterson

I use this one:

In a General Module:

Option Explicit
Function IsFormula(rng As Range)
IsFormula = rng(1).HasFormula
End Function
 
C

Chris Berry

Roger said:
What function can be used to determine whether a cell contains a
formula?

Thanks in advance for your help.

Roger

I picked up this from Jwalk.com and thought it was pretty neat.


Code:
--------------------
Sub QuickMap()
If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
' Create object variables for cell subsets
On Error Resume Next
Set FormulaCells = Range("A1").SpecialCells _
(xlFormulas, xlNumbers + xlTextValues + xlLogical)
Set TextCells = Range("A1").SpecialCells(xlConstants, xlTextValues)
Set NumberCells = Range("A1").SpecialCells(xlConstants, xlNumbers)
On Error GoTo 0

' Add a new sheet and format it
Sheets.Add
With Cells
.ColumnWidth = 2
.Font.Size = 8
.HorizontalAlignment = xlCenter
End With

Application.ScreenUpdating = False

' Do the formula cells
If Not IsEmpty(FormulaCells) Then
For Each Area In FormulaCells.Areas
With ActiveSheet.Range(Area.Address)
.Value = "F"
.Interior.ColorIndex = 3
End With
Next Area
End If

' Do the text cells
If Not IsEmpty(TextCells) Then
For Each Area In TextCells.Areas
With ActiveSheet.Range(Area.Address)
.Value = "T"
.Interior.ColorIndex = 4
End With
Next Area
End If

' Do the numeric cells
If Not IsEmpty(NumberCells) Then
For Each Area In NumberCells.Areas
With ActiveSheet.Range(Area.Address)
.Value = "N"
.Interior.ColorIndex = 6
End With
Next Area
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top