Solved

Execl - Adding user comments based on cell contents

Posted on 2016-11-15
5
42 Views
Last Modified: 2016-11-16
I have a spreadsheet with tabs for each month of a year and would like if a user enters "N" into a cell that Excel prompts the user to input a cell comment.  

Can someone help me with the VBA for this?

Thanks so much,

Mark
0
Comment
Question by:M Thomas
  • 3
  • 2
5 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41889078
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strComment As String

If UCase(Target) = "N" Then
    strComment = InputBox("Please enter the text of your comment")
    If Target.Comment Is Nothing Then
        Range(Target.Address).Comment.Text Text:=strComment
    Else
        If vbNo = MsgBox("A comment already exists in the cell. Do you want to replace it?", vbYesNo) Then
            Exit Sub
        Else
            Range(Target.Address).Comment.Text Text:=strComment
        End If
    End If
End If
End Sub

Open in new window

0
 

Author Comment

by:M Thomas
ID: 41890247
Thanks Martin.

I had the VBA go to debug on the following line:

Range(Target.Address).Comment.Text Text:=strComment

Can you help?

Thanks so much,

Mark
0
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41890395
I corrected that line and modified the code a bit for when there's already a comment in the cell.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strComment As String

If UCase(Target) = "N" Then
    If Target.Comment Is Nothing Then
        strComment = InputBox("Please enter the text of your comment")
        Range(Target.Address).AddComment strComment
    Else
        If vbNo = MsgBox("A comment already exists in the cell. Do you want to replace it?", vbYesNo) Then
            Exit Sub
        Else
            strComment = InputBox("Please enter the text of your new comment")
            Range(Target.Address).Comment.Text Text:=strComment
        End If
    End If
End If
End Sub

Open in new window

0
 

Author Closing Comment

by:M Thomas
ID: 41890414
Thanks so much Martin!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41890418
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
1

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question