VBA to find if a cell in a range has a comment

Printmark

New Member
Joined
Jan 22, 2010
Messages
7
I'm in need of a VBA Solution. I can't figure out how to have VBA check a named range "UnitPrice" to see if any of the cells in that range contain a comment. If not, then call module InsertRowsSP, if so, then display message box and end sub.

Basically, if the unit price has been lock or had a override applied (which adds a comment to the cell automatically), then no new rows can be added.

Below is the code I am trying, but its not working right.

Sub FindCommentsSp()
Dim Targetcells As Range
Dim MyRange As Range
Set MyRange = Range("SpUnitPrice")
On Error Resume Next
Selection.SpecialCells(xlCellTypeComments).Select
Set Targetcells = Selection
If Application.Intersect(MyCell, Targetcells) Is Nothing _
Then Call InsertRowsSP _
Else MsgBox ("Cannot add rows after price is locked or override is used")
Sheets("Sp Est").Activate
End Sub

Thanks
Mark
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Code:
Sub FindCommentsSp()
    Dim rngComments As Range
    
    On Error Resume Next
    Set rngComments = Range("SpUnitPrice").SpecialCells(xlCellTypeComments)
    On Error GoTo 0
    
    If rngComments Is Nothing Then
        Call InsertRowsSP
    Else
        MsgBox "Cannot add rows after price is locked or override is used"
    End If
    Sheets("Sp Est").Activate
    
End Sub
 
Upvote 0
Thanks Alpha Frog, your solution works great!

I knew I was making it way to complicated. Then I got fustrated and tried all kinds of weird stuff.

I appreciate the help!

Mark
 
Upvote 0
Upon futher testing, it didn't work all that great after all.. However, I did figure it out with a few minor changes.

Sub FindCommentsSp()
Dim rngComments As Object
On Error Resume Next
Selection.SpecialCells(xlCellTypeComments).Select
On Error Goto 0
Set rngComments = ActiveCell.Comment
If rngComments Is Nothing Then
Call InsertRowsSP
Else
MsgBox "Cannot add rows afert price is locked or override is used"
End If
End Sub

Sorry, don't know how to get it in one of those fancy code boxes. Whats missing is the selection of the Range("SpUnitPrice"), although that is range I wanted to check. When I added that in, I got errors, so I'm just letting it check the whole sheet that the range resides in. Not great, but it works.

Mark
 
Upvote 0

Forum statistics

Threads
1,224,589
Messages
6,179,744
Members
452,940
Latest member
rootytrip

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