PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 1.00 average.

Code for inserting Comment field with spreadsheet protection

 
 
buckag
Guest
Posts: n/a
 
      9th Jul 2008
I am a novice to VBA, but have included the following code to insert a
comment field with the date/time and user name:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Then
comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
Target.Cells.NoteText comment
End If
End Sub


I have a range of cells that are allow particular users to update these
cells. When I turn on the spreadsheet protection I am getting the following
error:

Runtime 1004 Note text Method of Range class failed, and the debugger takes
me to Target.Cells.NoteText comment.

My question is, is there additional code needed? Or is my code bad when
using spreadsheet protection? How can I correct my code?

Any help is appreciated

 
Reply With Quote
 
 
 
 
Gary Brown
Guest
Posts: n/a
 
      9th Jul 2008
Worked fine for me. To try something else, I would declare the comment and
change the variable to another name. Don't know if this will help but try...

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim strComment As String
If Target.Column = 1 Then
strComment = ("Cell Last Edited: ") & Now & (" by ") &
Application.UserName
Target.Cells.NoteText strComment
End If
End Sub

--
Good Luck and hope this helps.
If this post was helpfull, please remember to click on the ''''YES''''
button at the bottom of the screen.
Thanks,
Gary Brown


"buckag" wrote:

> I am a novice to VBA, but have included the following code to insert a
> comment field with the date/time and user name:
>
> Private Sub Worksheet_Change(ByVal Target As Excel.Range)
> If Target.Column = 1 Then
> comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
> Target.Cells.NoteText comment
> End If
> End Sub
>
>
> I have a range of cells that are allow particular users to update these
> cells. When I turn on the spreadsheet protection I am getting the following
> error:
>
> Runtime 1004 Note text Method of Range class failed, and the debugger takes
> me to Target.Cells.NoteText comment.
>
> My question is, is there additional code needed? Or is my code bad when
> using spreadsheet protection? How can I correct my code?
>
> Any help is appreciated
>

 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      9th Jul 2008

If Target.Count=1 And Target.Column = 1 Then
ActiveSheet.Unprotect
PutComment "Cell Last Edited: " & Now & " by " &
Application.UserName, Target
ActiveSheet.Protect
End If

Public Sub PutComment(txt As String, Optional Cell As Range)
'
' remove the old comment (if any)
'
If Cell Is Nothing Then
ActiveCell.ClearComments
Else
Cell.ClearComments
End If
'
' add a new comment
'
If txt <> "" Then
If Cell Is Nothing Then
ActiveCell.ClearComments
ActiveCell.AddComment
ActiveCell.Comment.Text txt
ActiveCell.Comment.Shape.TextFrame.AutoSize = Yes
Else
Cell.ClearComments
Cell.AddComment
Cell.Comment.Text txt
Cell.Comment.Shape.TextFrame.AutoSize = Yes
End If
End If
'
End Sub


"buckag" wrote:

> I am a novice to VBA, but have included the following code to insert a
> comment field with the date/time and user name:
>
> Private Sub Worksheet_Change(ByVal Target As Excel.Range)
> If Target.Column = 1 Then
> comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
> Target.Cells.NoteText comment
> End If
> End Sub
>
>
> I have a range of cells that are allow particular users to update these
> cells. When I turn on the spreadsheet protection I am getting the following
> error:
>
> Runtime 1004 Note text Method of Range class failed, and the debugger takes
> me to Target.Cells.NoteText comment.
>
> My question is, is there additional code needed? Or is my code bad when
> using spreadsheet protection? How can I correct my code?
>
> Any help is appreciated
>

 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      9th Jul 2008
I forgot to say, the PutComment sub needs to be placed in a standard module,
and change "Yes" to "True" (or put these two lines at the top of the standard
module)

Global Const Yes As Boolean = True
Global Const No As Boolean = False


"Charlie" wrote:

>
> If Target.Count=1 And Target.Column = 1 Then
> ActiveSheet.Unprotect
> PutComment "Cell Last Edited: " & Now & " by " &
> Application.UserName, Target
> ActiveSheet.Protect
> End If
>
> Public Sub PutComment(txt As String, Optional Cell As Range)
> '
> ' remove the old comment (if any)
> '
> If Cell Is Nothing Then
> ActiveCell.ClearComments
> Else
> Cell.ClearComments
> End If
> '
> ' add a new comment
> '
> If txt <> "" Then
> If Cell Is Nothing Then
> ActiveCell.ClearComments
> ActiveCell.AddComment
> ActiveCell.Comment.Text txt
> ActiveCell.Comment.Shape.TextFrame.AutoSize = Yes
> Else
> Cell.ClearComments
> Cell.AddComment
> Cell.Comment.Text txt
> Cell.Comment.Shape.TextFrame.AutoSize = Yes
> End If
> End If
> '
> End Sub
>
>
> "buckag" wrote:
>
> > I am a novice to VBA, but have included the following code to insert a
> > comment field with the date/time and user name:
> >
> > Private Sub Worksheet_Change(ByVal Target As Excel.Range)
> > If Target.Column = 1 Then
> > comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
> > Target.Cells.NoteText comment
> > End If
> > End Sub
> >
> >
> > I have a range of cells that are allow particular users to update these
> > cells. When I turn on the spreadsheet protection I am getting the following
> > error:
> >
> > Runtime 1004 Note text Method of Range class failed, and the debugger takes
> > me to Target.Cells.NoteText comment.
> >
> > My question is, is there additional code needed? Or is my code bad when
> > using spreadsheet protection? How can I correct my code?
> >
> > Any help is appreciated
> >

 
Reply With Quote
 
buckag
Guest
Posts: n/a
 
      9th Jul 2008
After I wrote this, I did think of turning off the password and turning it
back on as well. I did a little differently but it worked thanks to both
for your help, both ways are useful. It is so much fun to finally start
programming VBA than trouble shooting every couple years....

"Charlie" wrote:

> I forgot to say, the PutComment sub needs to be placed in a standard module,
> and change "Yes" to "True" (or put these two lines at the top of the standard
> module)
>
> Global Const Yes As Boolean = True
> Global Const No As Boolean = False
>
>
> "Charlie" wrote:
>
> >
> > If Target.Count=1 And Target.Column = 1 Then
> > ActiveSheet.Unprotect
> > PutComment "Cell Last Edited: " & Now & " by " &
> > Application.UserName, Target
> > ActiveSheet.Protect
> > End If
> >
> > Public Sub PutComment(txt As String, Optional Cell As Range)
> > '
> > ' remove the old comment (if any)
> > '
> > If Cell Is Nothing Then
> > ActiveCell.ClearComments
> > Else
> > Cell.ClearComments
> > End If
> > '
> > ' add a new comment
> > '
> > If txt <> "" Then
> > If Cell Is Nothing Then
> > ActiveCell.ClearComments
> > ActiveCell.AddComment
> > ActiveCell.Comment.Text txt
> > ActiveCell.Comment.Shape.TextFrame.AutoSize = Yes
> > Else
> > Cell.ClearComments
> > Cell.AddComment
> > Cell.Comment.Text txt
> > Cell.Comment.Shape.TextFrame.AutoSize = Yes
> > End If
> > End If
> > '
> > End Sub
> >
> >
> > "buckag" wrote:
> >
> > > I am a novice to VBA, but have included the following code to insert a
> > > comment field with the date/time and user name:
> > >
> > > Private Sub Worksheet_Change(ByVal Target As Excel.Range)
> > > If Target.Column = 1 Then
> > > comment = ("Cell Last Edited: ") & Now & (" by ") & Application.UserName
> > > Target.Cells.NoteText comment
> > > End If
> > > End Sub
> > >
> > >
> > > I have a range of cells that are allow particular users to update these
> > > cells. When I turn on the spreadsheet protection I am getting the following
> > > error:
> > >
> > > Runtime 1004 Note text Method of Range class failed, and the debugger takes
> > > me to Target.Cells.NoteText comment.
> > >
> > > My question is, is there additional code needed? Or is my code bad when
> > > using spreadsheet protection? How can I correct my code?
> > >
> > > Any help is appreciated
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting part of a Spreadsheet into the Current Spreadsheet nytwodees Microsoft Excel Misc 3 22nd Apr 2009 06:16 PM
Protection will not allow me to insert a comment =?Utf-8?B?V2lzaW5nIFVwIEdpcmxz?= Microsoft Excel Worksheet Functions 1 10th Apr 2006 08:43 PM
Protection will not allow me to insert a comment =?Utf-8?B?V2lzaW5nIFVwIEdpcmxz?= Microsoft Excel Misc 1 10th Apr 2006 08:33 PM
Help with inserting a comment =?Utf-8?B?QWw=?= Microsoft Access VBA Modules 2 13th Jan 2006 02:05 PM
Re: use PJL comment in Field code Cindy Meister -WordMVP- Microsoft Word Document Management 0 9th Sep 2003 07:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:05 PM.