For Each loop only works if I activate each cell in range

troyaaron

New Member
Joined
May 20, 2013
Messages
33
Hi all,

I hope someone can help me...

I have some code which looks at each cell in the selected range, and if the cell value is higher than that of row 3 of the same column, it will add a comment to that cell.

It currently works but only now I have made the loop activate each cell - but I'm sure this shouldn't be necessary. Can anyone see why it doesn't work when i take out the rCell.activate?
Code:
Sub ValueToComment()
Application.ScreenUpdating = False
Dim rCell As Range
Dim b As Range

For Each rCell In Selection
rCell.Activate
res = rCell.Offset(0, 1).Text
a = ActiveCell.Row

Set b = rCell.Offset((-a + 3), 0)

With rCell
If rCell.Value < b Then
On Error Resume Next
.Comment.Delete
On Error GoTo 0
.ClearComments
.AddComment
.Comment.Text Text:=res
.Comment.Visible = False
.Comment.Shape.TextFrame.AutoSize = True
Else
On Error Resume Next
.Comment.Delete
On Error GoTo 0
.ClearComments
' .AddComment
' .Comment.Text Text:=res
' .Comment.Visible = False
' .Comment.Shape.TextFrame.AutoSize = True
End If
End With
Next
Set rCell = Nothing
Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Im not sure if this will help or not, but I think this is because you have "For Each rCell In Selection" in the line above rcell.Activate.
 
Upvote 0
Hi Both,

@Charles

Yes that was already there...the a = ActiveCell.Row

is so i know how many rows to offset to get back to row 3

@Dustins

Should it be For Each cell in Selection (and not rCell)
 
Upvote 0
Change rcell.Activate to rcell.Select because you cant activate a range and you have this in place.. Dim rcell as Range
 
Upvote 0
ADVERTISEMENT
Hi Both,

@Charles

Yes that was already there...the a = ActiveCell.Row

is so i know how many rows to offset to get back to row 3

@Dustins

Should it be For Each cell in Selection (and not rCell)


a = rCell.Row

would work perfectly


But what is this trying to do

Set b = rCell.Offset((-a + 3), 0) ??
 
Last edited:
Upvote 0
Try this:
Code:
Sub ValueToComment()
    Dim rCell                      As Range
    Dim res                        As String

    Application.ScreenUpdating = False
    For Each rCell In Selection
        res = rCell.Offset(0, 1).Text

        With rCell
            If rCell.Value < Cells(3, rCell.Column).Value Then
                On Error Resume Next
                .Comment.Delete
                On Error GoTo 0
                .AddComment
                .Comment.Text Text:=res
                .Comment.Visible = False
                .Comment.Shape.TextFrame.AutoSize = True
            Else
                On Error Resume Next
                .Comment.Delete
                On Error GoTo 0
            End If
        End With
    Next
    Set rCell = Nothing
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
or this

Code:
Sub ValueToComment()
Dim rCell As Range
Dim b As Range
For Each rCell In Selection
    Set b = rCell.Offset((-rCell.Row + 3), 0)
    With rCell
        If .Value < b.Value Then
           .ClearComments
           .AddComment (.Offset(0, 1).Text)
          .Comment.Shape.TextFrame.AutoSize = True
        Else
         .ClearComments
        End If
    End With
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,196,280
Messages
6,014,433
Members
441,818
Latest member
itsfaisalkhalid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top