add comment to several WS's (2003 SP3)

steveh
SilverLounger
Posts: 1952
Joined: 26 Jan 2010, 12:46
Location: Nr. Heathrow Airport

add comment to several WS's (2003 SP3)

Post by steveh »

Morning all

I have a WB with about 270 WS's, on 265 of these sheets I would like to put the same comment in the same cell (D24), if I select all of these sheets the 'add comment' function is disabled, is there any way to do this rather than going through each one individually?
Steve
http://www.freightpro-uk.com" onclick="window.open(this.href);return false;
“Tell me and I forget, teach me and I may remember, involve me and I learn.”
― Benjamin Franklin

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: add comment to several WS's (2003 SP3)

Post by HansV »

You could modify this macro:

Code: Select all

Sub AddComment()
  Dim wsh As Worksheet
  Dim strText As String
  strText = "This is a comment"
  For Each wsh In ActiveWorkbook.Worksheets
    Select Case wsh.Name
      Case "Intro", "Summary", "Overview"
        ' Skip these sheets
      Case Else
        wsh.Range("D24").AddComment Text:=strText
    End Select
  Next wsh
End Sub
Make sure that only a single sheet is selected when you run this macro.
Best wishes,
Hans

steveh
SilverLounger
Posts: 1952
Joined: 26 Jan 2010, 12:46
Location: Nr. Heathrow Airport

Re: add comment to several WS's (2003 SP3)

Post by steveh »

HansV wrote:You could modify this macro:
Thanks Hans

That is great.

A tip though for any one using this, if you are stupid like me and run a test first using Han's test message 'This is a comment' a subsequent attempt will produce an error 1004 as it cannot overwrite the comment as is, therefore make sure that your comment is inserted when you run it, believe me it will work and does not need testing!!
Steve
http://www.freightpro-uk.com" onclick="window.open(this.href);return false;
“Tell me and I forget, teach me and I may remember, involve me and I learn.”
― Benjamin Franklin

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: add comment to several WS's (2003 SP3)

Post by HansV »

Here is a version that will delete existing comments before inserting new ones.

Code: Select all

Sub AddComment()
  Dim wsh As Worksheet
  Dim strText As String
  strText = "This is a comment"
  For Each wsh In ActiveWorkbook.Worksheets
    Select Case wsh.Name
      Case "Intro", "Summary", "Overview"
        ' Skip these sheets
      Case Else
        On Error Resume Next
        wsh.Range("D24").Comment.Delete
        On Error GoTo 0
        wsh.Range("D24").AddComment Text:=strText
    End Select
  Next wsh
End Sub
Best wishes,
Hans

steveh
SilverLounger
Posts: 1952
Joined: 26 Jan 2010, 12:46
Location: Nr. Heathrow Airport

Re: add comment to several WS's (2003 SP3)

Post by steveh »

Cheers Hans

That is going straight into my tips workbook, Thanks
Steve
http://www.freightpro-uk.com" onclick="window.open(this.href);return false;
“Tell me and I forget, teach me and I may remember, involve me and I learn.”
― Benjamin Franklin