VBA to open & centre comment on cell selection

men5j2s

Board Regular
Joined
Apr 26, 2016
Messages
59
Office Version
  1. 365
Platform
  1. Windows
hi,

The current code i have to make this work is as follows, but it is clunky and causing the worksheet to be incredibly slow.

Any ideas if i can achieve the same outcome in a simpler way?

I want column H to have cells that i can click and a comment box will instantly open up in the centre of the screen.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range
Dim cTop As Long
Dim cWidth As Long
Dim cmt As Comment
Dim Sh As Shape


Application.DisplayCommentIndicator _
= xlcommentIndicator0nly


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
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Add this to the beginning so it only works for column H:

Code:
If Not Intersect(Target, Range("H:H")) Is Nothing Then

'your code

End If
 
Upvote 0
Add this to the beginning so it only works for column H:

Code:
If Not Intersect(Target, Range("H:H")) Is Nothing Then

'your code

End If


Hi,

Thanks for the suggestion, this instantly helps navigation around other cells, but then as soon as i select a cell in H:H then the same issue occurs. Also with this modification the Comment does not disappear when i select another cell.
 
Upvote 0
Editing the shape is probably what takes so long... try stepping through the code and see how long each line takes to execute. Also, as far as the comment not disappearing when you select another cell, put this line:

Code:
Application.DisplayCommentIndicator _
= xlcommentIndicator0nly

before the "If not intersect" part, that way no matter where you click the comment will be hidden
 
Upvote 0
I would be careful using this code. While testing this it turned off my comments. I had to find the code to turn them back on.

Code:
Application.DisplayCommentIndicator _
= xlcommentIndicator0nly
 
Upvote 0
Editing the shape is probably what takes so long... try stepping through the code and see how long each line takes to execute. Also, as far as the comment not disappearing when you select another cell, put this line:

Code:
Application.DisplayCommentIndicator _
= xlcommentIndicator0nly

before the "If not intersect" part, that way no matter where you click the comment will be hidden

That code was already in the script and it was that code that was causing the issue (as discovered by stepping through.

The following code works well, but the comments never leave, they just overlay each other. there must be a way of sorting this?

the bizarre thing is that this used to work.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim rng As Range
Dim cTop As Long
Dim cWidth As Long
Dim cmt As Comment
Dim Sh As Shape


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
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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