![]()
Excel -- Comments -- Programming
You can add any of the following macros to a workbook that opens automatically when Excel opens (e.g. Personal.xls), then add a toolbar button or shortcut key to run it.
Change the User Name
Insert a Plain Comment
Insert a Formatted Comment
Insert a Comment with Date and Time
Replace Old Name in Comments
Reset Comments to Original Position
Resize Comments
Format All Comments
Show Comments on Active Sheet
Show Comment in Centre of Active Window
Copy Comment Text to Adjacent Cell
Copy Comments to Another Worksheet
Copy Comments from All Sheets to Another Worksheet
Copy Comments to Microsoft Word
Print Comments with Indicators
Number and List Comments
Create Comments with Pictures From File List
Insert Selected Picture Into Comment
Number and List CommentsWhen you print a worksheet that contains comments, you can use programming to number the comments. List the numbered comments on a separate sheet, and print them.
Download the zipped sample file for numbered comments: CommentsNumberPrint.zip
Draw Numbered Rectangles over the Comment Indicators
The following code will draw a numbered rectangle AutoShape over each comment indicator on the active sheet:
Sub CoverCommentIndicator() Dim ws As Worksheet Dim cmt As Comment Dim lCmt As Long Dim rngCmt As Range Dim shpCmt As Shape Dim shpW As Double 'shape width Dim shpH As Double 'shape height Set ws = ActiveSheet shpW = 8 shpH = 6 lCmt = 1 For Each cmt In ws.Comments Set rngCmt = cmt.Parent With rngCmt Set shpCmt = ws.Shapes.AddShape(msoShapeRectangle, _ rngCmt.Offset(0, 1).Left - shpW, .Top, shpW, shpH) End With With shpCmt With .Fill .ForeColor.SchemeColor = 9 'white .Visible = msoTrue .Solid End With With .Line .Visible = msoTrue .ForeColor.SchemeColor = 64 'automatic .Weight = 0.25 End With With .TextFrame .Characters.Text = lCmt .Characters.Font.Size = 5 .Characters.Font.ColorIndex = xlAutomatic .MarginLeft = 0# .MarginRight = 0# .MarginTop = 0# .MarginBottom = 0# .HorizontalAlignment = xlCenter End With .Top = .Top + 0.001 End With lCmt = lCmt + 1 Next cmt End SubRemove Rectangular AutoShapes over the Comment Indicators
The following code will remove the rectangular AutoShape over each comment indicator on the active sheet:
Sub RemoveIndicatorShapes() Dim ws As Worksheet Dim shp As Shape Set ws = ActiveSheet For Each shp In ws.Shapes If Not shp.TopLeftCell.Comment Is Nothing Then If shp.AutoShapeType = _ msoShapeRectangle Then shp.Delete End If End If Next shp End SubList Comments on New Sheet
The following code will list the numbered comments on a new worksheet:
Sub showcomments() 'posted by Dave Peterson 2003-05-16 Application.ScreenUpdating = False Dim commrange As Range Dim cmt As Comment Dim curwks As Worksheet Dim newwks As Worksheet Dim i As Long Set curwks = ActiveSheet On Error Resume Next Set commrange = curwks.Cells _ .SpecialCells(xlCellTypeComments) On Error GoTo 0 If commrange Is Nothing Then MsgBox "no comments found" Exit Sub End If Set newwks = Worksheets.Add newwks.Range("A1:D1").Value = _ Array("Number", "Name", "Value", "Comment") i = 1 For Each cmt In curwks.Comments With newwks i = i + 1 On Error Resume Next .Cells(i, 1).Value = i - 1 .Cells(i, 2).Value = cmt.Parent.Name.Name .Cells(i, 3).Value = cmt.Parent.Value .Cells(i, 4).Value = cmt.Parent.Address .Cells(i, 5).Value = Replace(cmt.Text, Chr(10), " ") End With Next cmt newwks.Cells.WrapText = False newwks.Columns.AutoFit Application.ScreenUpdating = True End Sub![]()
Create Comments with Pictures From File ListThe following code creates a comment with picture inserted, in column B, based on a file list in column A. Download the zipped sample file.
Sub InsertComment() Dim rngList As Range Dim c As Range Dim cmt As Comment Dim strPic As String On Error Resume Next Set rngList = Range("A1:A5") strPic = "C:\Data\" For Each c In rngList With c.Offset(0, 1) Set cmt = c.Comment If cmt Is Nothing Then Set cmt = .AddComment End If With cmt .Text Text:="" .Shape.Fill.UserPicture strPic & c.Value .Visible = False End With End With Next c End Sub![]()
Insert Selected Picture Into CommentThe following code creates a file from the selected picture, inserts it into a comment in the active cell, and deletes the picture. Download the zipped sample file.
Sub PictureIntoComment() Dim ch As ChartObject Dim dWidth As Double Dim dHeight As Double Dim ws As Worksheet Dim sName As String Dim cmt As Comment Dim sPath As String Dim sFile As String Dim rng As Range Set ws = ActiveSheet Set rng = ActiveCell sPath = ThisWorkbook.Path & "\" sName = InputBox("Name for picture file (no extension)", "File Name") If sName = "" Then sName = "Picture_" & Format(Date, "yyyymmdd") sFile = sPath & sName & ".gif" dWidth = Selection.Width dHeight = Selection.Height Selection.Cut Set ch = ws.ChartObjects.Add(Left:=rng.Left, Top:=rng.Top, _ Width:=dWidth, Height:=dHeight) ch.Chart.Paste rng.Activate ch.Chart.Export sFile ch.Delete Set cmt = rng.AddComment cmt.Text Text:="" With cmt.Shape .Fill.UserPicture sFile .Width = dWidth .Height = dHeight End With End Sub![]()
- Comments -- Basics
- Comments -- Tips
- Comments -- Programming
Last updated: July 14, 2008 8:28 PM