Modify insert comment code

Damo10

Active Member
Joined
Dec 13, 2010
Messages
460
Hi,

I have the following code to insert a comment into a protected workbook which works fine, what I would like to do is modify it so that it will only allow the user to enter a comment in the following range instead of any cell on the sheet.

Range ("J8:DM5038")

Code:
Sub InsertComment()
Dim xCom As String
Dim User As String
Dim xName As String
Dim Rng As Range
Dim c As Range
 
Set Rng = Sheet2.Range(Sheet2.Range("E6"), Sheet2.Range("E" & Rows.Count).End(xlUp))
User = UCase(Environ("username"))
If ActiveCell.Comment Is Nothing Then 'Checks to see if the cell already has a comment
For Each c In Rng
    If UCase(c) = User Then xName = c.Offset(, -2)
Next c
xTitleId = "Add Comment"
xCom = Application.InputBox("Enter comment", xTitleId, "", Type:=2)
With Application
    .ActiveSheet.Unprotect Password:="staff"
    .ActiveCell.AddComment
    .ActiveCell.Comment.Text Text:=xName & ":" & Chr(10) & xCom
    .ActiveSheet.Protect Password:="staff"
End With
Else
ActiveCell.Comment.Delete
For Each c In Rng
    If UCase(c) = User Then xName = c.Offset(, -2)
Next c
xTitleId = "Add Comment"
xCom = Application.InputBox("Enter comment", xTitleId, "", Type:=2)
With Application
    .ActiveSheet.Unprotect Password:="password"
    .ActiveCell.AddComment
    .ActiveCell.Comment.Text Text:=xName & ":" & Chr(10) & xCom
    .ActiveSheet.Protect Password:="password"
End With
End If
End Sub

Regards,
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
The comment is added to the active cell? If so, just add this line at the top of your macro:

Code:
If Intersect(ActiveCell, Range("J8:DM5038")) Is Nothing Then Exit Sub

You could add an error message if you want:

Code:
    If Intersect(ActiveCell, Range("J8:DM5038")) Is Nothing Then
        MsgBox "Invalid location for a comment."
        Exit Sub
    End If
 
Upvote 0
You could add this right at the start.
Code:
If Intersect(ActiveCell, Range("J8:DM5038")) Is Nothing Then
    MsgBox "You can only insert comments in the range J8:DM5038!", vbExclamation
    Exit Sub
End If
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,258
Members
449,307
Latest member
Andile

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