+ Reply to Thread
Results 1 to 12 of 12

Add coments VBA

Hybrid View

lasher18 Add coments VBA 05-11-2020, 05:15 AM
Kaper Re: Add coments VBA 05-11-2020, 05:29 AM
lasher18 Re: Add coments VBA 05-11-2020, 05:41 AM
Kaper Re: Add coments VBA 05-11-2020, 05:49 AM
lasher18 Re: Add coments VBA 05-11-2020, 05:58 AM
bakerman2 Re: Add coments VBA 05-11-2020, 06:31 AM
lasher18 Re: Add coments VBA 05-11-2020, 06:49 AM
Kaper Re: Add coments VBA 05-11-2020, 07:23 AM
lasher18 Re: Add coments VBA 05-11-2020, 07:58 AM
bakerman2 Re: Add coments VBA 05-11-2020, 09:01 AM
lasher18 Re: Add coments VBA 05-12-2020, 12:02 PM
bakerman2 Re: Add coments VBA 05-12-2020, 02:20 PM
  1. #1
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Add coments VBA

    Hi,
    I am trying to make a macro that insert automaticlly a comment if a cell match a certain condition. If there is a cell with the value "o" it should appear a comment box.
    So far I got this:
    Sub EjemploComentario()
    
    Dim rg As range
    Dim addcoment
    
    rg = Sheets("Leavetracker").range("B8:NH27")
    
    For Each cell In rg
    
    If cell = "o" Then
    ActiveCell.Select.addcoment
    
    End If
    
    Next cell
    
    End Sub
    Can you help me ?

    thanks
    Attached Images Attached Images

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Add coments VBA

    If cell = "o" Then
     With cell
      .AddComment
      .Comment.Text Text:="o"
      .Comment.Visible = True
     End With
    End If
    If there are comments already you may for instance override adding by
    If cell = "o" Then
     With cell
      On Error Resume Next
      .AddComment
      On Error Goto 0
      .Comment.Text Text:="o"
      .Comment.Visible = True
     End With
    End If
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Re: Add coments VBA

    Hi,
    thanks for your prompt reply, it returns me the following error in the capture attached

    Regards,
    Attached Images Attached Images

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Add coments VBA

    I've shown only the middle part to be inserted in your code. The whole sub could read:

    Sub EjemploComentario()
    
    Dim rg As Range, cell As Range
    
    rg = Sheets("Leavetracker").range("B8:NH27")
    
    For Each cell In rg
    
     If cell = "o" Then
      With cell
       On Error Resume Next
       .AddComment
       On Error Goto 0
       .Comment.Text Text:="o"
       .Comment.Visible = True
      End With
     End If
    Next cell
    
    End Sub

  5. #5
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Re: Add coments VBA

    where could be the mistake?
    Attached Images Attached Images

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,354

    Re: Add coments VBA

    Check if the sheetname is correct then try this way.

    Set rg = Sheets("Leavetracker").range("B8:NH27")
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Re: Add coments VBA

    Thanks guys,
    would there be some way to do it without having to run the macro every time? in the sense that every time an employee enters an "o" a new comment is automatically generated. Could be with an event like Worksheet_SelectionChange instead of sub?

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: Add coments VBA

    Yes, it could.

    But observing problems with the implementation - I'd strongly recommend following the hint from a yellow banner above.



    Anyway, let's try. The code for the Change event handler in a sheet (right click on the sheet name tab and select view code):

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    If not intersect(target,range("B8:NH27")) is nothing then
      For Each cell In intersect(target,range("B8:NH27"))
        If cell = "o" Then
          With cell
            On Error Resume Next
            .AddComment
            On Error Goto 0
            .Comment.Text Text:="o"
            .Comment.Visible = True
          End With
    ' may be here Else section to clear comment if there is no "o" in the cell
        End If
      Next cell
    End if
    End Sub

  9. #9
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Re: Add coments VBA

    Thank you!!
    just one more point: it would be ideal to not show all the comments at the same time, just the one in the cell selected.
    This I know can be achieved writting: .Comment.Visible = False ,but in this way it does not allow me to edit the comment. I'd need both see the comment just in the selected cell and able to edit it.

    Would be target.comment.visible=true ?

    I attached you the template with the code this time.

    thanks a lot
    Attached Files Attached Files

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,354

    Re: Add coments VBA

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B8:NH27")) Is Nothing Then
        If Target = "o" Then
            With Target
                On Error Resume Next
                .AddComment
                On Error GoTo 0
                With .Comment
                    .Text Text:="o"
                    .Visible = True
                End With
            End With
          ElseIf Target = vbNullString Then
            Target.Comment.Delete
          End If
    End If
    End Sub

  11. #11
    Registered User
    Join Date
    04-02-2020
    Location
    Bratislava
    MS-Off Ver
    2016
    Posts
    80

    Re: Add coments VBA

    Amazing, thanks very much man

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,354

    Re: Add coments VBA

    You're welcome and thanks for rep+.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Cell coments into Word
    By screetch in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-11-2007, 04:01 AM
  2. How do I ensure that the coments appear adjacent to the cell
    By A.RAGHUNATH in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-07-2006, 08:40 AM
  3. Can I change Indacator coments color?
    By Monica in forum Excel General
    Replies: 2
    Last Post: 02-16-2006, 05:10 PM
  4. [SOLVED] Coments
    By Horacio in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-22-2005, 09:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1