Create comment if cell value is larger than column
It can sometimes be helpful having a large cell value in a comment. You can then easily hover over cell and read the value instead of resizing column widths.
How to run macro?
- Press Alt + F8 to open the Macro dialog box.
- Press with mouse on macro Createcomments to select it.
- Press with left mouse button on "Run" button to run the selected macro.
- An input box shows up on your screen asking for a cell range that you want to apply comments to.
Only cells containing values that don't fit will get a comment. - Press with left mouse button on OK button.
- The macro adds comments.
- The macro ends.
The animated image below shows what happens when you run macro Createcomments.
VBA Code
The following subroutine adds a comment to cells where the value exceeds the column width.
'Name macro Sub Createcomments() 'Dimension variables and declare data types Dim rng As Range Dim Cell As Variant 'Enable error handling On Error Resume Next 'Show inputbox and save input value to object variable rng Set rng = Application.InputBox(Prompt:="Select a range:", _ Title:="Create comments in cells where the value is larger than column width", _ Default:=Selection.Address, Type:=8) 'Disable error handling On Error GoTo 0 'Check if variable rang is empty If rng Is Nothing Then 'Continue if variable rng is not empty Else 'Iterate through cells in object rng For Each Cell In rng 'Check if characters in cell value is wider than column width If Len(Cell) * 0.9 > Cell.ColumnWidth Then 'Check that there is no comment to prevent overwriting older comments If Cell.Comment Is Nothing Then 'Add value to cell comment Cell.AddComment Cell.Value End If End If Next Cell End If End Sub
Where to put the code?
- Copy above VBA code.
- Press Alt+F11 to open the VB Editor.
- Select your workbook in the Project Explorer.
- Press with mouse on "Insert" on the menu.
- Press with mouse on "Module" to insert a code module to your workbook.
- Paste VBA code to code module.
- Exit VB Editor and return to Excel.
Macro category
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
Vba category
Today I'll show you how to search all Excel workbooks with file extensions xls, xlsx and xlsm in a given folder for a […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
The macro demonstrated above creates hyperlinks to all worksheets in the current worksheet. You will then be able to quickly […]
Excel categories
2 Responses to “Create comment if cell value is larger than column”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Thank you so much for such a clear explanation. I actually was looking for code to resize a column if cell contents were beyond a certain size, and your explanation was so clear, it made it easy for me (a beginner) to modify it for my purposes. Really appreciate it!
I used this and it worked great! Only question is how do I refresh the data. I am using this code on a dynamic excel sheet that is updated daily and I want the comments to update accordingly?