VBA to copy Cell.Formula to cell comment

AusSteelMan

Board Regular
Joined
Sep 4, 2009
Messages
208
Hi everyone,

I have had a shot at some code that will check if a cell in the selected range has a formula and if so add this formula as a text string in the same cell's comments.

Code:
Public Sub CellFormulaToCellComment()

Dim CellInRange As Range
'Dim CellComment as String


For Each CellInRange In Selection
    If CellInRange.HasFormula Then
        CellComment = cell.Formula    '"this is a formula"
        CellInRange.ClearComments
        CellInRange.AddComment (CellComment)
    End If
Next
    
End Sub

When I run it i get a "Run-time error '424': Object required on the "CellComment = cell.Formula" line.
If I replace the cell.Formula with the string shown above, everything works.
So I know where my problem is, but apparently not knowledgeable enough to fix it (or I do know but am having a moment of fogginess :))

I tried commenting out the Dim CellComment As String in case that was contributing too. No such simple luck.

Can anyone please correct my code as I got sick of manually copy and pasting?

Many thanks in advance
Cheers,
Darren
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
The line:
CellComment = cell.Formula
should be:
CellComment = CellInRange.Formula
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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