VBA to AutoSize comments in cells

Kra

Board Regular
Joined
Jul 4, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to set up a macro which will loop through all sheets and autosize all comments and change their properties to Move but don't size with cells. I created test comment and macro, but somehow it is not working (no errors, macro goes smoothly without changing anything in the sheet). Any ideas how to fix it and how to achieve this Move but don't size with cells? I was not able to set it up
1670412892207.png


VBA Code:
Private Sub CommandButton2_Click()

Dim ws As Worksheet
Dim com As Comment
Dim rng As range
Dim celcom As range


    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
            Set rng = ws.UsedRange
                For Each celcom In rng
                    If Not celcom.Comment Is Nothing Then
            
                            celcom.Comment.Shape.TextFrame.AutoSize = True
                            celcom.Comment.Shape.Top = celcom.Parent.Top + 15
                            celcom.Comment.Shape.Left = celcom.Parent.Offset(0, 1).Left + 15
                    
                    End If
                Next celcom
    Next ws
        
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
I managed to fix code, but I still cannot set the settings of comment to Move but don't size with cell
VBA Code:
Dim ws As Worksheet
Dim com As Comment
Dim rng As range
Dim celcom As range


    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
            Set rng = ws.UsedRange
                For Each celcom In rng
                    If Not celcom.Comment Is Nothing Then
            
                            celcom.Comment.Shape.TextFrame.AutoSize = True
                    
                    End If
                Next celcom
                
                For Each com In ActiveSheet.Comments
                    com.Shape.Top = com.Parent.Top + 15
                    com.Shape.Left = com.Parent.Offset(0, 1).Left + 15
                Next com
    Next ws
 
Upvote 0
Solution
I managed to fix code, but I still cannot set the settings of comment to Move but don't size with cell
VBA Code:
Dim ws As Worksheet
Dim com As Comment
Dim rng As range
Dim celcom As range


    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
            Set rng = ws.UsedRange
                For Each celcom In rng
                    If Not celcom.Comment Is Nothing Then
           
                            celcom.Comment.Shape.TextFrame.AutoSize = True
                   
                    End If
                Next celcom
               
                For Each com In ActiveSheet.Comments
                    com.Shape.Top = com.Parent.Top + 15
                    com.Shape.Left = com.Parent.Offset(0, 1).Left + 15
                Next com
    Next ws
Ok I fixed it as well, added line
VBA Code:
com.Shape.Placement = xlMove
to loop
VBA Code:
For each com
 
Upvote 0

Forum statistics

Threads
1,215,331
Messages
6,124,311
Members
449,152
Latest member
PressEscape

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