Excel Comments VBA

Página 1

Search Contextures Sites

Search

Create Timeline Chart Make Perfect Timelines Fast See Examples. Free Download!
www.SmartDraw.com

Gantt Chart Online Easy . Quick. Visual. Cost -Effective Loved By Thousands of Managers.
www.Wrike .com

Project Gantt Charts Drag/Drop Schedules. Team Builder. Project Worksheets. View Demo.
www.attask.com

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

Change the User Name
Instead of showing the user name at the start of Excel comments, you can change to something generic, such as "Note:" However, this change affects the User Name in all Microsoft Office programs , so you may want to reset the name before you exit Excel.

To set a generic label in Excel comments: Sub CommentNote() Application.UserName = "Note" End Sub To reset the User Name in Excel comments : Sub CommentName() Application.UserName = "John Smith" End Sub

Insert a Plain Comment
To insert a comment with no User Name, use the following macro. Note: Because the macro contains a SendKeys command, it should be run with the worksheet active, not Visual Basic Explorer.

Sub CommentAddOrEdit() 'adds new plain text comment or positions 'cursor at end of existing comment text

http://www.contextures.com/xlcomments03.html

09/11/2010 01:14:25 p.m.

contextures. and the text will be added to the comment box.Visible = True cmt. strOld.contextures.Comment If cmt Is Nothing Then Set cmt = ActiveCell.Delete cmt. Their name may also appear in the Status Bar. when you hover over the cell that contains a comment.text text:="" End If 'type to add comment text to selected shape cmt. Página 2 Sub CommentAddOrEdit() 'method suggested by Jon Peltier 2006-03-04 'adds new plain text comment or adds text 'at end of existing comment text Dim cmt As Comment Set cmt = ActiveCell.AddComment cmt. which leaves the Excel comments visible.html Dim cmt As Comment Set cmt = ActiveCell.com\xlcomments03. strNew) cmt. .m. Start typing.AddComment text :=strComment Next cmt Next ws End Sub http://www.com\xlcomments03. the comment shape is selected. After running the macro.Worksheets For Each cmt In ws.Parent.html Dim ws As Worksheet Dim cmt As Comment Dim strOld As String Dim strNew As String Dim strComment As String strNew = "New Name " strOld = "Old Name " Application. their name may appear at the top of the comment.Comments strComment = Replace(cmt .text.Comment If cmt Is Nothing Then ActiveCell. you can use the following variation . or to the end of the existing comment text.AddComment text :="" End If SendKeys "%ie~" End Sub To avoid use of the SendKeys command.com/xlcomments03. Sub ChangeCommentName() 'replaces old names in Excel comments 'deletes and reinserts Excel comments ' so new name appears in status bar 'www.UserName = strNew For Each ws In ActiveWorkbook.Excel Comments VBA 'www.Shape. The following macro will replace the old name with a new name.contextures.Select End Sub Replace Old Name in Excel Comments If a previous user inserted comments.html 09/11/2010 01:14:25 p.

Comment With cmt.contextures.text.Name = "Times New Roman" . cmt.Comment End If 'find the line break and markers lBreak = InStr(1. strFind) + 1 'format the lines of text http://www.Size = 11 .contextures. strFind) + 1 lNum2 = InStr(lBreak. blue text in the second line. use the following macro: Sub CommentAddOrEditTNR () 'adds TimesNewRoman comment or positions 'cursor at end of existing comment text 'www.Comment If cmt Is Nothing Then ActiveCell. cmt.html 09/11/2010 01:14:25 p.html Dim cmt As Comment Dim str 1 As String Dim str 2 As String Dim lBreak As Long Dim lNum 1 As Long Dim lNum 2 As Long Dim lNumLen As Long Dim strFind As String On Error Resume Next str1 = "John : 20 Eggs" str2 = "Simon: 50 Eggs " strFind = ":" lNumLen = 3 Set cmt = ActiveCell.com/xlcomments03. use the following macro: Sub CommentTextFormatColour() 'adds comment then formats font colour and adds bold 'www. formatted in Times New Roman font.AddComment text :="" Set cmt = ActiveCell. .TextFrame.text .com\xlcomments03. formatted with red text in the first line .text.AddComment _ text:=str1 & Chr(10) & str2 Set cmt = ActiveCell.Shape. Chr(10)) lNum1 = InStr(1.Bold = False .com\xlcomments03.html Dim cmt As Comment Set cmt = ActiveCell.Font .Comment If cmt Is Nothing Then ActiveCell.Characters.contextures.ColorIndex = 0 End With End If SendKeys "%ie~" End Sub Ads by Google Excel VBA Exercises Excel Programming Excel Macro Training Excel 2007 Insert a Colour Formatted Comment To insert a comment with no User Name.m. and bold text after the colons. cmt .Excel Comments VBA Página 3 Insert a Formatted Comment To insert Excel comments with no User Name .

contextures. lNumLen).com/xlcomments03.text text :=Format(Now.Characters(lNum 1.ColorIndex = 3 . strDate) & Chr (10) Else cmt.Font.html 09/11/2010 01:14:25 p.Parent.Characters(lNum 2. 1).Characters.Left + 5 http://www.html Dim strDate As String Dim cmt As Comment strDate = "dd-mmm-yy hh:mm:ss" Set cmt = ActiveCell. you can reset them using the following code: Sub ResetComments() Dim cmt As Comment For Each cmt In ActiveSheet.Shape.Characters(1.Characters.Bold = True .Font .Bold = False End With SendKeys "%ie~" End Sub Reset Comments to Original Position If Excel comments have moved out of position.Comment If cmt Is Nothing Then Set cmt = ActiveCell. strDate) & Chr(10) End If With cmt.Bold = False .ColorIndex = 5 End With 'add bold to numbers that follow colon If lNum 1 > 0 Then With cmt.Excel Comments VBA With cmt.Shape.Top = cmt.text & Chr(10) _ & Format(Now.Characters(lBreak + 1. . or append the current date and time to an existing comment. ' positions cursor at end of comment text 'www.Comments cmt.com\xlcomments03.m.text text :=cmt .Font . lNumLen).Offset(0.TextFrame . lBreak).TextFrame .Font .Top + 5 cmt.Left = _ cmt.Font. use the following macro: Sub CommentDateTimeAdd() 'adds Excel comments with date and time .Shape. Len(cmt .AddComment cmt.contextures.TextFrame .Shape.text )) .Shape.Parent.Font .Bold = True End With End If SendKeys "%ie~" End Sub Página 4 Insert Excel Comments with Date and Time To insert Excel comments with the current date and time.

Cells If Not (mycell.Comment .AutoSize = True If .Shape.Shape.AutoSize = True If .Width = 200 .Width > 300 Then lArea = .com/xlcomments03. and the second macro resizes all comments in the selected range.Shape.Height .Shape. you can use the following code to change the font and font size for all comments in the workbook.1 seems to work ok.2 End If End With End If Next mycell End Sub Ads by Google Excel Download Excel Excel MS Excel Sheet Excel Experts Format All Excel Comments After you have inserted comments in a workbook. Resize all comments on the active sheet Sub Comments_AutoSize() 'posted by Dana DeLouis 2000 -09-16 Dim MyComments As Comment Dim lArea As Long For Each MyComments In ActiveSheet.html Dim ws As Worksheet Dim cmt As Comment For Each ws In ActiveWorkbook .Height .Shape.Worksheets http://www.Height = (lArea / 200) * 1. The first macro resizes all comments on the active sheet.Comment Is Nothing) Then With mycell.TextFrame .Shape.TextFrame .Shape. Sub FormatAllComments() 'www.Width = 200 ' An adjustment factor of 1. you can reset them using the following code.m.1 End If End With Next ' comment End Sub Resize all comments in the selected area Sub ResizeCommentsInSelection() 'Posted by Dave Peterson 2002 -02-25 Dim mycell As Range Dim myRng As Range Dim lArea As Long Set myRng = Selection For Each mycell In myRng.Shape.Comments With MyComments .Height = (lArea / 200) * 1.Shape.com\xlcomments03.Width * . .contextures.contextures.Width > 300 Then lArea = . .Excel Comments VBA Next End Sub Página 5 Resize Excel Comments If Excel comments have changed size.Shape.html 09/11/2010 01:14:25 p.Shape.Shape.Width * .

Comment Set sh = cmt .com/xlcomments03.Name = "Times New Roman" .Top = cTop .Width / 2 If ActiveCell.sh.sh.html 09/11/2010 01:14:25 p. If a cell with a comment is selected on that sheet. and display the comment indicators only on other sheets.Visible = True End If End Sub Show Excel Comments at Right of Active Window http://www. Instead.Width / 2 cmt. all comments in all open workbooks will be displayed.Size = 12 End With Next cmt Next ws End Sub Página 6 Show Comments on Active Sheet If you choose View|Comments.m. Private Sub Worksheet _SelectionChange (ByVal Target As Range) 'www.Left = cWidth .TextFrame .contextures.DisplayCommentIndicator _ = xlCommentIndicatorOnly Set rng = ActiveWindow.contextures.Shape.VisibleRange cTop = rng.Excel Comments VBA For Each cmt In ws.html Dim rng As Range Dim cTop As Long Dim cWidth As Long Dim cmt As Comment Dim sh As Shape Application.contextures.com\xlcomments03.Comments With cmt.Height / 2 cWidth = rng . you can use code to show the Excel comments on one sheet.Comment Is Nothing Then 'do nothing Else Set cmt = ActiveCell.Visible = True Next End Sub Show Excel Comments in Centre of Active Window Paste the following code onto a worksheet module.Left + rng .Top + rng .Shape sh. .Height / 2 sh.Characters. Sub ShowSheetComments() 'www.Font .html 'shows all comments on the active sheet Dim c As Comment For Each c In ActiveSheet. its comment is displayed in the centre of the active window's visible range.Comments c.com/xlcomments03.

Visible = True End If End Sub Copy Comment Text to Adjacent Cell The following macro will copy comment text to the cell to the right.ScreenUpdating = False Dim commrange As Range Dim mycell As Range Dim curwks As Worksheet Set curwks = ActiveSheet On Error Resume Next Set commrange = curwks. If a cell with a comment is selected on that sheet.com/xlcomments03.Text End If Next mycell Application.Comment Is Nothing Then 'do nothing Else Set cmt = ActiveCell.Excel Comments VBA Paste the following code onto a worksheet module.Left = rng.m.Value = mycell.Value = "" Then mycell.ScreenUpdating = True End Sub http://www.sh. A bit of space is added (lGap) to allow for scroll bar on the right side.Top = cTop .Offset(0.contextures.VisibleRange cTop = rng. Página 7 Private Sub Worksheet _SelectionChange (ByVal Target As Range) 'www.Shape sh. its comment is displayed at the far right of the active window's visible range. if that cell is empty.Height / 2 lGap = 30 'adjust space between window edge and comment If ActiveCell. Sub ShowCommentsNextCell() 'based on code posted by Dave Peterson 2003-05-16 Application.Top + rng .com/xlcomments03. 1).Width .lGap cmt. .Height / 2 sh.sh. 1).Offset(0.html 09/11/2010 01:14:25 p.Comment Set sh = cmt .contextures.Width .SpecialCells(xlCellTypeComments ) On Error GoTo 0 If commrange Is Nothing Then MsgBox "no comments found" Exit Sub End If For Each mycell In commrange If mycell.Cells _ .Comment.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.

Value = _ Array("Address".ScreenUpdating = True End Sub Copy Comments from All Sheets to Another Worksheet The following macro will add a sheet to the workbook. including the cell address.Name .Cells _ . 3). 1).Cells(i.Address .Excel Comments VBA Página 8 Copy Comments to Another Worksheet The following macro will add a sheet to the workbook.Comment.Text End With Next mycell Application.Range("A1:D1"). if any. including the sheet name. .contextures.com/xlcomments03. 2).ScreenUpdating = False Dim Dim Dim Dim Dim commrange mycell As curwks As newwks As i As Long As Range Range Worksheet Worksheet Set curwks = ActiveSheet On Error Resume Next Set commrange = curwks.Cells(i.Value = mycell.Value = mycell. Sub ShowCommentsAllSheets() 'modified from code 'posted by Dave Peterson 2003 -05-16 Application.Cells(i.Value = mycell.Value = mycell.ScreenUpdating = False Dim commrange As Range Dim mycell As Range http://www.Cells(i.Add newwks.m.SpecialCells(xlCellTypeComments) On Error GoTo 0 If commrange Is Nothing Then MsgBox "no comments found" Exit Sub End If Set newwks = Worksheets. with a list of Excel comments from all sheets in the workbook. cell address.Name. with a list of comments.html 09/11/2010 01:14:25 p. if any. Sub showcomments() 'posted by Dave Peterson 2003 -05-16 Application. and cell name. 4). and cell name. "Comment") i = 1 For Each mycell In commrange With newwks i = i + 1 On Error Resume Next .Value . "Name". "Value".

Value = mycell. 2). "Address". remove line break newwks.text End With Next mycell End If Set commrange = Nothing Next ws 'format cells for no wrapping . 5). "Name".SpecialCells(xlCellTypeComments) On Error GoTo 0 If commrange Is Nothing Then 'do nothing Else i = newwks. .Name.Cells.Documents . "Value".Address . MatchCase :=False.contextures.com\xlcomments03.Value = mycell. 1).contextures.Add DocumentType:=0 For Each cmt In ActiveSheet.Clear Set WdApp = CreateObject("Word .Replace What :=Chr (10).Comment.Cells.Value = _ Array("Sheet". "Comment") For Each ws In ActiveWorkbook.Name .End (xlUp). _ SearchFormat:=False.Visible = True .html Dim cmt As Comment Dim WdApp As Object On Error Resume Next Set WdApp = GetObject (.Cells(i. and adds it to a Microsoft Word document.WrapText = False newwks.Row For Each mycell In commrange With newwks i = i + 1 On Error Resume Next . 3).Value = ws.Cells(i.Value = mycell. ReplaceFormat:=False Application. _ SearchOrder:=xlByRows . Sub CopyCommentsToWord() 'www.com/xlcomments03. 4).Count.Comments http://www.Value = mycell.Value . LookAt:=xlPart.Add newwks.Cells(Rows .Worksheets On Error Resume Next Set commrange = ws. 1). along with the cell address.Excel Comments VBA Dim ws As Worksheet Dim newwks As Worksheet Dim i As Long Set newwks = Worksheets. "Word .m.Cells(i.Name .Cells(i. _ Replacement:=" ".Application") End If With WdApp .Application") If Err .Columns("E:E").Number <> 0 Then Err.ScreenUpdating = True End Sub Página 9 Copy Excel Comments to Microsoft Word The following code copies the comment text from the active sheet.Cells(i.Range("A1:E1").html 09/11/2010 01:14:25 p.

contextures.Selection.com\xlcomments03.Flip msoFlipHorizontal .Comment Is Nothing Then http://www.Visible = msoFalse End With Next cmt End Sub Remove Triangular AutoShapes over the Comment Indicators The following code will remove the triangular AutoShape over each comment indicator on the active sheet: Sub RemoveIndicatorShapes() 'www. As a workaround .ForeColor . There is no option to change this behaviour .Text .m. .Excel Comments VBA .TypeParagraph Next End With Set WdApp = Nothing End Sub Página 10 Print Worksheet with Comment Indicators When you print a worksheet that contains comments.Comments Set rngCmt = cmt .Top. you can draw triangle AutoShapes over the comment indicators .Visible = msoTrue .Shapes If Not shp . shpH ) End With With shpCmt .html 09/11/2010 01:14:25 p.Address _ & vbTab & cmt.Offset(0.TypeText cmt .Line .Shapes.Parent.AddShape (msoShapeRightTriangle.html Dim ws As Worksheet Dim cmt As Comment Dim rngCmt As Range Dim shpCmt As Shape Dim shpW As Double 'shape width Dim shpH As Double 'shape height Set ws = ActiveSheet shpW = 6 shpH = 4 For Each cmt In ws.com/xlcomments03. 57=Green . _ rngCmt.com\xlcomments03.contextures.Solid .contextures. shpW.Left .TopLeftCell.Flip msoFlipVertical .Parent With rngCmt Set shpCmt = ws.shpW. the comment indicators are not visible.html Dim ws As Worksheet Dim shp As Shape Set ws = ActiveSheet For Each shp In ws.Fill .Selection.Fill .Fill . 1).SchemeColor = 10 'Red '12=Blue . . Draw Triangular AutoShapes over the Comment Indicators The following code will draw a triangular AutoShape over each comment indicator on the active sheet: Sub CoverCommentIndicator() 'www.

MarginBottom = 0# .001 http://www. Download the zipped sample file for numbered comments in Excel 2003 and earlier versions: CommentsNumberPrint. .Line .html 09/11/2010 01:14:25 p.Offset(0.SchemeColor = 64 'automatic . and print them .Characters.m.Excel Comments VBA If shp .SchemeColor = 9 'white .contextures.Text = lCmt .Shapes.25 End With With .ColorIndex = xlAutomatic .Visible = msoTrue .Font .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 shpW = shpH = lCmt = = ActiveSheet 8 6 1 For Each cmt In ws.Left .Characters.Weight = 0.Comments Set rngCmt = cmt . 1). _ rngCmt.Font .Top + 0. List the numbered comments on a separate sheet.Size = 5 .TextFrame .MarginRight = 0# .contextures.Top.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. . shpH ) End With With shpCmt .MarginTop = 0# .MarginLeft = 0# .Visible = msoTrue .Parent With rngCmt Set shpCmt = ws.AddShape (msoShapeRectangle.HorizontalAlignment = xlCenter End With .com\xlcomments03.Delete End If End If Next shp End Sub Página 11 Number and List Excel Comments When you print a worksheet that contains comments.Top = . shpW .ForeColor.ForeColor.Characters.Name = "CmtNum" & .Name With .Fill .com/xlcomments03.Solid End With With . you can use programming to number the comments. Please use this file : CommentNumbersPrint2007.shpW.zip NOTE : Code is slightly different for Excel 2007 .AutoShapeType = _ msoShapeRightTriangle Then shp.

" ") End With http://www.m.Name. Chr (10).Comments With newwks i = i + 1 On Error Resume Next .Parent.contextures.Name . "Value". 3).Shapes If Not shp .Parent.Text .com\xlcomments03. "Address".contextures. 6) = "CmtNum" Then shp.Value = i .Address . "Name ".Value .Value = _ Array("Number".Delete End If End If Next shp End Sub List 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.Name .Cells(i.html Dim ws As Worksheet Dim shp As Shape Set ws = ActiveSheet For Each shp In ws.com/xlcomments03. "Comment") i = 1 For Each cmt In curwks.Comment Is Nothing Then If Left (shp.Cells(i.SpecialCells(xlCellTypeComments) On Error GoTo 0 If commrange Is Nothing Then MsgBox "no comments found" Exit Sub End If Set newwks = Worksheets.Value = cmt .ScreenUpdating = False Dim Dim Dim Dim Dim commrange As Range cmt As Comment curwks As Worksheet newwks As Worksheet i As Long Set curwks = ActiveSheet On Error Resume Next Set commrange = curwks.Excel Comments VBA End With lCmt = lCmt + 1 Next cmt End Sub Remove Rectangular AutoShapes over the Comment Indicators The following code will remove the rectangular AutoShape over each comment indicator on the active sheet: Página 12 Sub RemoveIndicatorShapes() 'www.Value = Replace(cmt. .Value = cmt .Add newwks.1 .Cells(i. 2).Cells(i.Cells(i.Cells _ . 4).Parent. 1).TopLeftCell.Value = cmt .html 09/11/2010 01:14:25 p. 5).Range("A1:E1").

inserts it into a comment in the active cell.AddComment End If With cmt .Comment If cmt Is Nothing Then Set cmt = . "File Name") http://www.contextures.UserPicture strPic & c.Text Text:="" .html 09/11/2010 01:14:25 p.AutoFit Application.Offset(0.Fill . and deletes the picture.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.ScreenUpdating = True End Sub Página 13 Create Excel Comments with Pictures From File List The following code creates a comment with picture inserted.com/xlcomments03. Sub InsertComment() 'www.Value .WrapText = False newwks.Columns. in column B.Shape.Excel Comments VBA Next cmt newwks.Path & "\" sName = InputBox("Name for picture file (no extension )".contextures. 1) Set cmt = c. Download the zipped sample file.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.contextures.Visible = False End With End With Next c End Sub Insert Selected Picture Into Comment The following code creates a file from the selected picture. .Cells. Download the zipped sample file. based on a file list in column A. Sub PictureIntoComment() 'www.com\xlcomments03.m.

Copyright ©2010 All rights reserved.Chart.Chart.Tips 3.ChartObjects.contextures. Excel Comments -.Basics 2. Height:=dHeight) ch. Debra Dalgleish Last updated: October 2.Delete Set cmt = rng.Top.m.Export sFile ch..Left . 2010 10:46 PM http://www.Excel Comments VBA If sName = "" Then sName = "Picture_" & Format(Date. Excel Comments -.Activate ch.UserPicture sFile .com/xlcomments03.Text Text:="" With cmt.Width = dWidth .Fill.Add (Left:=rng.Width dHeight = Selection .Cut Set ch = ws. Top :=rng.VBA Home Excel Tips Excel Files The Excel Store Blog Contact Privacy Policy Contextures Inc.html 09/11/2010 01:14:25 p.Paste rng.Shape . _ Width:=dWidth. Excel Comments -. .gif" dWidth = Selection.Height = dHeight End With End Sub Página 14 1.AddComment cmt. "yyyymmdd") sFile = sPath & sName & ".Height Selection .

Sign up to vote on this title
UsefulNot useful