PDA

View Full Version : Solved: VBA Code - Adding value to Comments



dakkat
05-04-2010, 11:30 AM
Hello,

I have been struggling with this section of code. I am populating values to a spreadsheet based upon raw data on another sheet. I get the field data to appear but am unable to add a value to the comments section of the same field. The code runs and adds the comment triangle in the first cell but does not copy any data and gives the following error:

Run-time error '1004':
Copy Method of Worksheet Class failed

The code for this particular function is below:

Dim LR As Long, i As Long, X As Variant
With Sheets("rd")
LR = .Range("N" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("N" & i)
X = Application.Match(.Value, Sheets("overview").Range("B1:B500"), 0)
If IsNumeric(X) Then .Offset(, 22).Copy Destination:=Sheets("overview").Cells(X, Columns.Count).End(xlToLeft).Offset(, 1)
If IsNumeric(X) Then .Offset(, -12).Copy Destination:=Sheets("overview").Cells(X + 1, Columns.Count).End(xlToLeft).Offset(, 1)
If IsNumeric(X) Then .Offset(, -11).Copy Destination:=Sheets("overview").Cells(X + 1, Columns.Count).End(xlToLeft).Offset(0, 0).AddComment.Text
End With
Next i
End With


First one copies the score value to the correct cell. Second copy adds the ID code to the cell directly below the score. Third (one having issues) is suppose to add its value into the comment of the second (ID code) cell.

If I remove the 'AddComment.Text' from the code, it will copy the value to the cell. I need to copy the value into the comment of the cell.

Can anyone shed some light here? Any help would be greatly appreciated.

Bob Phillips
05-04-2010, 11:51 AM
How about this



Dim LR As Long, i As Long, X As Variant
With Sheets("rd")
LR = .Range("N" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With .Range("N" & i)
X = Application.Match(.Value, Sheets("overview").Range("B1:B500"), 0)
If IsNumeric(X) Then .Offset(, 22).Copy Destination:=Sheets("overview").Cells(X, Columns.Count).End(xlToLeft).Offset(, 1)
If IsNumeric(X) Then .Offset(, -12).Copy Destination:=Sheets("overview").Cells(X + 1, Columns.Count).End(xlToLeft).Offset(, 1)
If IsNumeric(X) Then Sheets("overview").Cells(X + 1, Columns.Count).End(xlToLeft).AddComment .Offset(, -11).Text
End With
Next i
End With

dakkat
05-04-2010, 11:56 AM
That is perfect. I have a follow up question. Is there a way to add default text before the pasted text. For example, this function is adding a date to the comments:

5/1/2010

Could I have a generic text string added prior to this date?

Submission Date: 5/1/2010

Bob Phillips
05-04-2010, 12:09 PM
This should do it



If IsNumeric(X) Then Sheets("overview").Cells(X + 1, Columns.Count).End(xlToLeft).AddComment "Submission Date: " & .Offset(, -11).Text

dakkat
05-04-2010, 12:14 PM
Thank you so much! This is going to save me a lot of time.

Cheers!

mdmackillop
05-04-2010, 12:51 PM
Hi dakkat,
Welcome to VBAX
If this is Solved, please mark it so using the Thread Tools dropdown
Regards
MD