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

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
The line:
CellComment = cell.Formula
should be:
CellComment = CellInRange.Formula
 
Upvote 0
Solution

Forum statistics

Threads
1,224,455
Messages
6,178,772
Members
452,875
Latest member
Disastrouscoder

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