Hello Bope,
The period prefix indicates the property or method is belongs to a With ... With End statement. You could use With in the loop like this...
For Each Res In Wks.Range(Cells(StRow, "D"), Cells(rowloop - 1 < "D"))
With Res
.AddComment
.Comment.Text Text:=Comtxt
End With
Next Res
Another problem you can encounter is trying to Add a comment to cell that already has a comment. You should check before doing so. If the comment exists then change the Comment's text to the new text. I modified your code to this.
Private Sub Comment()
Dim Comm As Comment
Dim LastRow As Long
Dim StartRow As Long
Dim StRow As Long
Dim rowloop As Long
Dim Wks As Worksheet
Set Wks = Worksheets("test")
StartRow = 3
LastRow = Wks.Cells(Rows.Count, "D").End(xlUp).Row
For Each Comp In Wks.Range(Cells(StartRow, "D"), Cells(LastRow, "D"))
Select Case Comp
Case Is = "Sa"
StRow = Row.Count + 1
rowloop = StRow
Do While Cells(rowloop, "D").Value <> ""
rowloop = rowloop + 1
Loop
Comtxt = Cells(rowloop, "B").Value
For Each Res In Wks.Range(Cells(StRow, "D"), Cells(rowloop - 1 < "D"))
Set Comm = Res.Comment
If Not Comm Is Nothing Then
Comm.Text = ComTxt
Else
Res.AddComment
Res.Comment.Text = Comtxt
End If
Next Res
End Select
Next Comp
End Sub
Sincerely,
Leith Ross
Bookmarks