Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Adding a comment to a cell in VBA?!

The dox are quite clear, do this...

ActiveSheet.Range(DestCPr & ocount).AddComment "Formula price"

But this gives me an "Application defined error".

Anyone have an idea?

Maury
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Adding a comment to a cell in VBA?!

The first thought that hit me was that DestCPr and oCount didn't make a nice
address.

The second thought was that that cell already had a comment in it.

But this worked ok for me:

Option Explicit
Sub testme01()

Dim DestCPr As String
Dim oCount As Long

DestCPr = "A"
oCount = 1

With ActiveSheet.Range(DestCPr & oCount)
.ClearComments
.AddComment "Formula price"
End With
End Sub



Maury Markowitz wrote:

The dox are quite clear, do this...

ActiveSheet.Range(DestCPr & ocount).AddComment "Formula price"

But this gives me an "Application defined error".

Anyone have an idea?

Maury


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Adding a comment to a cell in VBA?!

Two possibilities:

1. The expression "DestCPr & ocount" doesn't evaluate to
something that looks like a range.

2. The cell in question already contains a comment. In this
case you can do something like:

If Not ActiveSheet.Range(DestCPr & ocount).Comment Is Nothing Then
ActiveSheet.Range(DestCPr & ocount).Comment.Delete
End If

HTH
Andrew taylor


Maury Markowitz wrote:
The dox are quite clear, do this...

ActiveSheet.Range(DestCPr & ocount).AddComment "Formula price"

But this gives me an "Application defined error".

Anyone have an idea?

Maury


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default Adding a comment to a cell in VBA?!

Dave's beaten me to it but here's a similar code

Sub addComment()
Dim c
For Each c In Selection
c.addComment "Test Comment" & Chr(10) _
& "Line 2"
Next c
End Sub

Peter Atherton



"Maury Markowitz" wrote:

The dox are quite clear, do this...

ActiveSheet.Range(DestCPr & ocount).AddComment "Formula price"

But this gives me an "Application defined error".

Anyone have an idea?

Maury

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Adding a comment to a cell in VBA?!

"Dave Peterson" wrote:

The first thought that hit me was that DestCPr and oCount didn't make a nice
address.


No, that's not it. The line immediately before it is identical with the
exception that it says .value = "10", which works fine.

The second thought was that that cell already had a comment in it.


This appears to be the problem. But the mystery simply deepens.

This is a brand new spreadsheet, created every day from scratch in the code.
No other code adds a comment, and in fact the sheet that's being worked on
didn't even exist 10 seconds earlier.

Maury


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Adding a comment to a cell in VBA?!

Are you sure the correct sheet is active?

Is that activesheet protected?

What else does the error message say?

Maury Markowitz wrote:

"Dave Peterson" wrote:

The first thought that hit me was that DestCPr and oCount didn't make a nice
address.


No, that's not it. The line immediately before it is identical with the
exception that it says .value = "10", which works fine.

The second thought was that that cell already had a comment in it.


This appears to be the problem. But the mystery simply deepens.

This is a brand new spreadsheet, created every day from scratch in the code.
No other code adds a comment, and in fact the sheet that's being worked on
didn't even exist 10 seconds earlier.

Maury


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Adding a comment to a cell in VBA?!

"Dave Peterson" wrote:

Are you sure the correct sheet is active?


Yes.

Is that activesheet protected?


No.

What else does the error message say?


Nothing, just the generic error, whatever it was.

Maury
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Adding a comment to a cell in VBA?!

Sorry, I don't have another guess.

Maury Markowitz wrote:

"Dave Peterson" wrote:

Are you sure the correct sheet is active?


Yes.

Is that activesheet protected?


No.

What else does the error message say?


Nothing, just the generic error, whatever it was.

Maury


--

Dave Peterson
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Adding a comment to a cell in VBA?!

Try something like this. It might give you some insight:

Sub EFGH()
Dim rng As Range
Dim cmt As Comment
Dim DestCPR As String
Dim ocount As Long

DestCPR = "A"
ocount = 10
On Error Resume Next
Set rng = Nothing
Set cmt = Nothing
Set rng = ActiveSheet.Range(DestCPR & ocount)
If rng Is Nothing Then
MsgBox "Invalid range"
Else
Set cmt = Nothing
Set cmt = rng.Comment
If Not cmt Is Nothing Then
MsgBox "cmt exist: " & cmt.Text
Exit Sub
End If
Set cmt = Nothing
Set cmt = rng.AddComment("Formula price")
If cmt Is Nothing Then
MsgBox "Range Good, Comment Bad"
End If
End If
On Error GoTo 0

End Sub

--
Regards,
Tom Ogilvy



"Maury Markowitz" wrote in
message ...
"Dave Peterson" wrote:

Are you sure the correct sheet is active?


Yes.

Is that activesheet protected?


No.

What else does the error message say?


Nothing, just the generic error, whatever it was.

Maury



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a hyper link to a cell comment Christopher Buxton Excel Discussion (Misc queries) 6 June 25th 07 07:20 PM
Adding a comment to a cell mancitmis New Users to Excel 2 November 3rd 05 08:52 AM
adding comment to cell mancitmis Excel Discussion (Misc queries) 1 November 2nd 05 03:15 PM
Adding a comment to a formula-not a cell Floridagal Excel Discussion (Misc queries) 4 January 6th 05 12:23 AM
Adding Comment to cell Troy H Excel Programming 5 May 14th 04 09:31 AM


All times are GMT +1. The time now is 04:25 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"