Solved

Execl - Adding user comments based on cell contents

Posted on 2016-11-15
5
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 47

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 47

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 47

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

726 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