Search Contextures Sites ![]()
Excel Comments VBA
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 Colour Formatted Comment
Insert Excel Comments with Date and Time
Replace Old Name in Excel Comments
Reset Comments to Original Position
Resize Excel Comments
Format All Excel Comments
Show Comments on Active Sheet
Show Excel Comments in Centre of Active Window
Show Excel Comments at Right of Active Window
Copy Comment Text to Adjacent Cell
Copy Comments to Another Worksheet
Copy Comments from All Sheets to Another Worksheet
Copy Excel Comments to Microsoft Word
Print Comments with Indicators
Number and List Excel Comments
Create Excel Comments with Pictures From File List
Insert Selected Picture Into Comment
Show Excel Comments in Centre of Active Window
Paste the following code onto a worksheet module. If a cell with a comment is selected on that sheet, its comment is displayed in the centre of the active window's visible range.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'www.contextures.com/xlcomments03.html Dim rng As Range Dim cTop As Long Dim cWidth As Long Dim cmt As Comment Dim sh As Shape Application.DisplayCommentIndicator _ = xlCommentIndicatorOnly Set rng = ActiveWindow.VisibleRange cTop = rng.Top + rng.Height / 2 cWidth = rng.Left + rng.Width / 2 If ActiveCell.Comment Is Nothing Then 'do nothing Else Set cmt = ActiveCell.Comment Set sh = cmt.Shape sh.Top = cTop - sh.Height / 2 sh.Left = cWidth - sh.Width / 2 cmt.Visible = True End If End Sub![]()
Show Excel Comments at Right of Active Window
Paste the following code onto a worksheet module. If a cell with a comment is selected on that sheet, its comment is displayed at the far right of the active window's visible range. A bit of space is added (lGap) to allow for scroll bar on the right side.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'www.contextures.com/xlcomments03.html
'show comments at centre right of window Dim rng As Range Dim cTop As Long Dim lGap As Long Dim cmt As Comment Dim sh As Shape Application.DisplayCommentIndicator _ = xlCommentIndicatorOnly Set rng = ActiveWindow.VisibleRange cTop = rng.Top + rng.Height / 2 lGap = 30 'adjust space between window edge and comment If ActiveCell.Comment Is Nothing Then 'do nothing Else Set cmt = ActiveCell.Comment Set sh = cmt.Shape sh.Top = cTop - sh.Height / 2 sh.Left = rng.Width - sh.Width - lGap cmt.Visible = True End If End Sub![]()
Number and List Excel 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 in Excel 2003 and earlier versions: CommentsNumberPrint.zip
NOTE: Code is slightly different for Excel 2007. Please use this file: CommentNumbersPrint2007.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() 'www.contextures.com\xlcomments03.html 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 .Name = "CmtNum" & .Name 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() 'www.contextures.com\xlcomments03.html 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 Left(shp.Name, 6) = "CmtNum" 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:E1").Value = _ Array("Number", "Name", "Value", "Address", "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 Excel Comments with Pictures From File List
The 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() 'www.contextures.com\xlcomments03.html 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() 'www.contextures.com\xlcomments03.html 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![]()
- Excel Comments -- Basics
- Excel Comments -- Tips
- Excel Comments -- VBA
Contextures Inc., Copyright © 2009.
All rights reserved.
Last updated: February 6, 2010 10:57 PM