simple range selection returning "run time error 91" randomly...

sadams1

Board Regular
Joined
Aug 19, 2006
Messages
219
hello & many thanks for taking time to see if I'm crazy or not...attached are 2 screenshots...one of the code, the other of the error...& all this is is a way to select a range & resize the comment box. It works 100% when only 1 cell is selected. It works randomly wiith more than one cell selected. It will occasionally work with an entire column selected. It must have to do with the cell formatting so I've messed with that & unable to get anything to work consistently. Thank you again for any insights.
 

Attachments

  • z2.png
    z2.png
    71.9 KB · Views: 16
  • z.png
    z.png
    46.4 KB · Views: 15

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
That happens when you have no comment in the selected range. Add this line of code to your macro and add this UDF, like this:
VBA Code:
Option Explicit
Sub cmtHideMakeSmall()
    '
    ' MacroHIDE Macro
    '
    ' Keyboard Shortcut: Ctrl+w
    '
    Dim r     As Range
    If Not bHasComment(Selection) Then Exit Sub   '<- added
    ActiveCell.Comment.Visible = False
    For Each r In Selection
        If Not r.Comment Is Nothing Then
            With r.Comment.Shape
                .Width = 502
                .Height = 150
            End With
        End If
    Next r
End Sub

Function bHasComment(cell As Range) As Boolean
    On Error Resume Next
    bHasComment = cell.Comment.Parent.Address = cell.Address
End Function
 
Upvote 0
greatly appreciate the help...still messing with it to see if I'm not doing something right to get it to work.
 
Upvote 0
Is Run-time error #91 resolved? It should've been. To work, the macro and the UDF go in the same module; if separated, the UDF goes into a standard module.
 
Upvote 0
What I'm finding is:
1. Run time error resolved
2. code works for one cell
3. the comment boxes aren't adjusting size if the range more than 1 cell

What I did was to copy the code from the browser right into a new module so not fat fingers....I think this is what was meant by having the macro & UDF in the same module?
 
Upvote 0
Patch the macro as follows. The UDF doesn't need any modification and yes, it goes where you intended.
VBA Code:
Option Explicit
Sub cmtHideMakeSmall()
    '
    ' MacroHIDE Macro
    '
    ' Keyboard Shortcut: Ctrl+w
    '
    Dim r     As Range
    For Each r In Selection
        If Not bHasComment(r) Then GoTo skip      '<- moved and changed
        r.Comment.Visible = False                 '<- moved and changed
        If Not r.Comment Is Nothing Then
            With r.Comment.Shape
                .Width = 502
                .Height = 150
            End With
        End If
skip:                                             '<- added
    Next r
End Sub
 
Upvote 0
Solution
That's it! Amazing as it makes something horrendously tedious into one macro. Can't thank you enough!
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,096
Latest member
Anshu121

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