Excel

Find Formulas with Comments

Ease of Use

Intermediate

Version tested with

2000, 2002 

Submitted by:

Justinlabenne

Description:

Adds a comment to each cell containing a formula. Comment will contain the text "Formula is: " and then the formula formatted as text. Ex: Formula is: =SUM(A1:A10) 

Discussion:

Helps to keep track of any added or removed formulas on your sheets so you can locate them with ease, plus view the formula that is in the cell. If formulas are removed, re-run the code and all previous comments in formual cells are cleared, then re-added to all current formula cells. All other comments are left intact, and you will be prompted if you want to overwrite any comments you have previously added into formula cells. 

Code:

instructions for use

			

Option Explicit Sub FormulasInComment() Const CmtString As String = "Formula is:" Dim r As Range Dim MyRange As Range Dim MyComment ' Prompt to run procedure (yes/no) If MsgBox("Find all formula cells by adding a comment?" & vbCr & _ "You will be prompted if you want to overwrite previous comments" & vbCr & _ "ALL comments NOT in formula cells will be left intact" _ , vbYesNo, "Comments") = vbNo Then Exit Sub ' Specify the range Set MyRange = ActiveSheet.UsedRange ' Clear previous formula comments ' Only clear formula comments other than this procedure by prompt ' Leave all other comments intact On Error Resume Next For Each r In MyRange If Left(r.Comment.Text, 11) = CmtString And Not (r.HasFormula) Then r.ClearComments End If If Left(r.Comment.Text, 11) <> CmtString And r.HasFormula Then r.Select If MsgBox("The comment in Cell " & r.Address(0, 0) & vbCr & _ "Contains the text:" & vbCr & _ "" & r.Comment.Text & "" & vbCr & vbCr & "Overwrite this comment?", _ vbYesNo) = vbYes Then r.ClearComments End If End If Next r ' Now add comments to cells containing formulas ' Display the formula in the comment For Each r In MyRange ' Check for comment MyComment = r.Comment.Text If r.HasFormula = True And IsEmpty(MyComment) Then r.AddComment "Formula is: " & r.Formula ' Autosize and format With r.Comment.Shape .TextFrame.AutoSize = True .AutoShapeType = msoShapeRoundedRectangle .Shadow.Type = msoShadow12 .Line.Weight = 1# End With ' Set comment to indicator only r.Comment.Visible = False End If ' Clear for next test MyComment = Nothing Next r ' Clear memory Set MyRange = Nothing End Sub

How to use:

  1. Open the Excel Workbook you want to add the code to
  2. Press ALT + F11 to enter the VBE
  3. From the Menu bar at the top
  4. INSERT > MODULE
  5. Paste this code into the open white window that appears
  6. Press ALT + Q to return to Excel
 

Test the code:

  1. If you don't have formulas on your sheet, add some for trialing this code
  2. Go to TOOLS > MACRO > MACROS
  3. When the Dialog-Box appears find -FormulasInComment-
  4. Highlight it and press the run button on the right
  5. Comment indicators should appear on all cells with formulas
 

Sample File:

CmntFormula.zip 9.99KB 

Approved by mdmackillop


This entry has been viewed 107 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express