Excel

Add comments to cells by double clicking the cell.

Ease of Use

Easy

Version tested with

2003 

Submitted by:

malik641

Description:

This will prompt you with an input box of what text you want in the comment. It will also add more lines to an existing comment. It also sizes the comment for you. 

Discussion:

Instead of clicking Insert --> Comment... or right-clicking the cell and selecting "Insert Comment" and then sizing the comment manually, this code will do it for you. It will add comments to cells and add additional text to existing comments and it autosizes the comment for you. It will also display the comment once you select the cell and hide it when you move to another cell. 

Code:

instructions for use

			

'Code to be placed in the worksheet(s) you want to use this in Option Explicit Public oldRange As Range Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) On Error Resume Next Dim rng As Range Set rng = Target(1, 1) oldRange.Comment.Visible = False With rng If Not .Comment Is Nothing Then If .Comment.Visible = False Then .Comment.Visible = True Else .Comment.Visible = False End If End If End With Set oldRange = Target(1, 1) End Sub Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean) On Error Resume Next Dim cmtText As String Dim inputText As String If Target.Comment Is Nothing Then cmtText = InputBox("Enter info:", "Comment Info") If cmtText = "" Then Exit Sub Target.AddComment Text:=cmtText Target.Comment.Visible = True Target.Comment.Shape.TextFrame.AutoSize = True 'Remove if you want to size it yourself Else If Target.Comment.Text <> "" Then inputText = InputBox("Enter info:", "Comment Info") If inputText = "" Then Exit Sub cmtText = Target.Comment.Text & Chr(10) & inputText Else cmtText = InputBox("Enter info:", "Comment Info") End If Target.ClearComments Target.AddComment Text:=cmtText Target.Comment.Visible = True Target.Comment.Shape.TextFrame.AutoSize = True 'Remove if you want to size it yourself End If Cancel = True 'Remove this if you want to enter text in the cell after you add the comment End Sub

How to use:

  1. Right-click the worksheet tab you want the code to be placed in
  2. Click on View Code
  3. Paste the code in the Sheet's module
  4. Close the VBE
 

Test the code:

  1. Double click on a cell
  2. Write the text to be placed in the comment
  3. Press Enter
 

Sample File:

Add Ongoing Comments.zip 9.35KB 

Approved by mdmackillop


This entry has been viewed 250 times.

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