Macro applying modification to comment on selected cells

brucewflee

New Member
Joined
Nov 8, 2014
Messages
12
Hi,

I have been using the following macro to edit the font and size of my comment box.

Code:
Sub FixComments()


        Dim MyComments As Comment
        For Each MyComments In ActiveSheet.Comments
            
            With MyComments
            .Shape.TextFrame.AutoSize = True
            If .Shape.Width > 300 Then
                Dim lArea As Long
                lArea = .Shape.Width * .Shape.Height
                .Shape.Width = 250
                .Shape.Height = (lArea / 200) * 1.1
            End If
            End With
            With MyComments.Shape.TextFrame.Characters.Font
                .Name = "Tahoma"
                .Size = 10
                .Bold = False
            End With
        Next MyComments


    End Sub

However, with my sheet getting larger and larger, the time it is being used when I run this macro is getting longer. Is there a way I can select the cells I want to apply the macro instead to speed up the process?

I have tried using "For each cell in Selection.Cells" but it is still running the whole sheet unfortunately.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I just tested on a sheet with > 200 comments and it took 46 seconds to run your macro! Not sure why I can only assume that comments are resource heavy..:confused:
 
Upvote 0
You need to disable screen updating to get a decent time saving. Try this:
Code:
Sub FixComments()


Dim d As Date
Dim r As Range


        d = Now
        Application.ScreenUpdating = False
        On Error Resume Next
        For Each r In Selection
            If Not r.Comment Is Nothing Then
                With r.Comment
                .Shape.TextFrame.AutoSize = True
                    If .Shape.Width > 300 Then
                        Dim lArea As Long
                        lArea = .Shape.Width * .Shape.Height
                        .Shape.Width = 250
                        .Shape.Height = (lArea / 200) * 1.1
                    End If
                End With
                With r.Comment.Shape.TextFrame.Characters.Font
                    .Name = "Tahoma"
                    .Size = 10
                    .Bold = False
                End With
            End If
            'DoEvents
        Next r
        Application.ScreenUpdating = True


    MsgBox "Time taken = " & DateDiff("s", d, Now) & " seconds", vbInformation, "Processed " & Selection.Cells.Count & " cells"
    
    End Sub
 
Upvote 0
Thanks Gallen. It works perfect.

My original macro does have the screen updating disabled. I have only posted the main bit here just to avoid confusion.

So it is the bit "For Each r In Selection" which makes it work right. How it is different from Selection.Cells just wondering?
 
Upvote 0
r is dimmed as a range. As far as I'm aware, there should be no difference. I've just tested it and getting the same time to loop through over 200 comments with Selection and Selection.Cells so I'm a little confused if it slows yours down.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,402
Members
449,156
Latest member
LSchleppi

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