VBA to close comment box

Shadkng

Active Member
Joined
Oct 11, 2018
Messages
365
Hi, I got the below code from the internet which I then modified a bit, which opens a comment box. Is it possible to have the box close when clicking on another cell? Thanks

VBA Code:
Sub addNewComment()
    Selection.Interior.ColorIndex = 44  'interior color of selected cell
    ActiveCell.AddComment _
   
                With ActiveCell.Comment
            
                With .Shape
                    .AutoShapeType = msoShapeFoldedCorner
                    .Fill.ForeColor.RGB = RGB(215, 224, 239)
                    .Width = 150
                    .Height = 75
                    
                    With .TextFrame
                        .Characters.Font.Size = 11
                        .Characters.Font.Name = "Calibri"
                    End With
                End With
                .Visible = True
                .Shape.Select True
            End With
            End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Woudn't it be easier to simply only have the comment visible when the cell is selected??
Remove these 2 lines from your code
VBA Code:
                '.Visible = True
                '.Shape.Select True
 
Upvote 0
It will open when you reselect the cell
 
Upvote 0
The original code allows me to click a cell which then brings up the comment box ready to type which is exactly what I want. What you suggested adds the comment to the cell but the box never opens. So my goal is to have the box close when I click away after I am done typing. If that is not possible then can the it be made to toggle the box open and close?
 
Upvote 0
Hi,​
to close try to set Visible to False at least …​
 
Upvote 0
Not near the computet at the moment. ..but you would need to use a worksheet selection change event
 
Upvote 0
I'm not super clear on what you want, but maybe something like this. It uses the double click event to add a new comment.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Cells.Count = 1 Then
        If Target.Comment Is Nothing Then
            addNewComment
        End If
    End If
End Sub

Sub addNewComment()
    ActiveCell.Interior.ColorIndex = 44                'interior color of selected cell
    ActiveCell.AddComment
    With ActiveCell.Comment
        With .Shape
            .AutoShapeType = msoShapeFoldedCorner
            .Fill.ForeColor.RGB = RGB(215, 224, 239)
            .Width = 150
            .Height = 75

            With .TextFrame
                .Characters.Font.Size = 11
                .Characters.Font.Name = "Calibri"
            End With
        End With
        .Visible = True
        .Shape.Select True
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim C As Comment

    For Each C In Me.Comments
        C.Visible = False
        C.Parent.Interior.ColorIndex = -4142
    Next C
End Sub
 
Upvote 0
Solution
I'm not super clear on what you want, but maybe something like this. It uses the double click event to add a new comment.

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Cells.Count = 1 Then
        If Target.Comment Is Nothing Then
            addNewComment
        End If
    End If
End Sub

Sub addNewComment()
    ActiveCell.Interior.ColorIndex = 44                'interior color of selected cell
    ActiveCell.AddComment
    With ActiveCell.Comment
        With .Shape
            .AutoShapeType = msoShapeFoldedCorner
            .Fill.ForeColor.RGB = RGB(215, 224, 239)
            .Width = 150
            .Height = 75

            With .TextFrame
                .Characters.Font.Size = 11
                .Characters.Font.Name = "Calibri"
            End With
        End With
        .Visible = True
        .Shape.Select True
    End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim C As Comment

    For Each C In Me.Comments
        C.Visible = False
        C.Parent.Interior.ColorIndex = -4142
    Next C
End Sub
Can you tell me where put the above code? Does it go in a module or in the sheet code? Thanks
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,933
Members
449,134
Latest member
NickWBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top