macro to run in active cell only, not whole worksheet

Annie62

New Member
Joined
Dec 14, 2018
Messages
4
Hi All,

I grabbed this macro code, and it works perfectly, except for one thing, the macro runs on the whole worksheet and I need it to run on only the active cell.

I’ve tried adjusting ActiveWorkbook, with ActiveCell, Worksheet with Cell, and a few other things, but nothing seems to work (I’m still a newbie). My thinking was I need to recode something(s) in the first several lines of the below code. Any help is greatly appreciated.


Sub CommentTheHeckOuttaIt()
Dim ws As Worksheet
Dim iCell As Range
For Each ws In ActiveWorkbook.Worksheets
For Each iCell In ws.UsedRange
With iCell
If CStr(.Value) <> "" Then
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=CStr(.Value)
.Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
.Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
End If
If .Formula <> "" Then
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=CStr(.Formula)
.Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
.Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
End If
End With
Next
Next
End Sub


What I am trying to accomplish is…
Run the macro on single/active cell (example C8), then hover over the comment box for cell (C8), the comment box for cell then shows me the text that is in that specific cell.

Thank You,
Annie62
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
try this:
Code:
Sub CommentTheHeckOuttaIt()
Dim ws As Worksheet
Dim iCell As Range
'For Each ws In ActiveWorkbook.Worksheets
'For Each iCell In ws.UsedRange
With Selection
If Selection.Cells.Count = 1 Then
If CStr(.Value) <> "" Then
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=CStr(.Value)
.Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
.Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
End If
If .Formula <> "" Then
.ClearComments
.AddComment
.Comment.Visible = False
.Comment.Text Text:=CStr(.Formula)
.Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
.Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
End If
End If
End With
'Next
'Next
End Sub
 
Upvote 0
or this
Code:
Sub CommentTheHeckOuttaIt()
    With ActiveCell
        If CStr(.Value) <> "" Then
            .ClearComments
            .AddComment
            .Comment.Visible = False
            .Comment.Text Text:=CStr(.Value)
            .Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
            .Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
        End If
        If .Formula <> "" Then
            .ClearComments
            .AddComment
            .Comment.Visible = False
            .Comment.Text Text:=CStr(.Formula)
            .Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
            .Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Perhaps this:-
NB:- Change Target.address in code to suit.!!
NB:- This is a selection Change event, Place in Activesheet Module :- Alt+F11> Vb window appears > Paste in Vbwindow.
Code:
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_SelectionChange(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]If[/COLOR] Target.Address(0, 0) = "C8" [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]With[/COLOR] Target
    [COLOR="Navy"]If[/COLOR] CStr(.Value) <> "" [COLOR="Navy"]Then[/COLOR]
        .ClearComments
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:=CStr(.Value)
        .Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
        .Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]If[/COLOR] .HasFormula [COLOR="Navy"]Then[/COLOR]
        .ClearComments
        .AddComment
        .Comment.Visible = False
        .Comment.Text Text:=CStr(.Formula)
        .Comment.Shape.ScaleWidth 3.87, msoFalse, msoScaleFromTopLeft
        .Comment.Shape.ScaleHeight 2.26, msoFalse, msoScaleFromTopLeft
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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