VBA: How to check if cell contains Formula

sunilbsrv2k

Board Regular
Joined
May 25, 2018
Messages
73
Hi,

I am trying to search through a range of cells, if any cell contains formula. If the cell contains formula, it has to pop-up "Yes".

But I am getting Error as Object Required. Please help.

here is my code:

VBA Code:
Sub chkform()
Rng = Range("R8:R16")
For Each cell In Rng
    If cell.HasFormula = True Then
        MsgBox "Yes"
    End If
Next cell
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
VBA Code:
Option Explicit

Sub chkform()
Dim cell As Range
Dim rng As Range
Set rng = Range("R8:R16")
For Each cell In rng
    If cell.HasFormula = True Then
        MsgBox "Yes"
    End If
Next cell
End Sub
 
Upvote 0
VBA Code:
Option Explicit

Sub chkform()
Dim cell As Range
Dim rng As Range
Set rng = Range("R8:R16")
For Each cell In rng
    If cell.HasFormula = True Then
        MsgBox "Yes"
    End If
Next cell
End Sub

Awesome.... Its working fine... Thank you so much
 
Upvote 0
Hi, I have one more question with respect to "For each cell".

How to copy an entire row from this loop and paste it in another sheet.

Thanks
 
Upvote 0
Just wanted to update this question, maybe I am not clear.

I am using the loop :
VBA Code:
For Each cell In rng
    If cell.HasFormula = True Then
        MsgBox "Yes"
    End If
Next cell

In this loop, if I want to capture the entire row where "cell.HasFormula = True", how to get that?

Please help.

Thanks,
 
Upvote 0
Something like?.....
VBA Code:
Sub chkform()
Dim cell As Range, rng As Range
Set rng = Range("R8:R16")

For Each cell In rng
    If cell.HasFormula = True Then cell.EntireRow.Interior.ColorIndex = 6
Next cell
End Sub
 
Upvote 0
Hi
VBA Code:
Sub chkform()
    Dim cell As Range
    Dim rng As Range
    Dim r As Range
    Set rng = Range("R8:R16")
    For Each cell In rng
        If cell.HasFormula = True Then
            If Not r Is Nothing Then
                Set r = Union(r, cell.EntireRow)
            Else: Set r = cell.EntireRow
            End If
        End If
    Next cell
    r.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,338
Members
451,637
Latest member
hvp2262

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