Code to remove comment from dynamic validation range

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Code to remove comment from dynamic validation range

Post by ABabeNChrist »

I have this great piece of code (Mostly from Hans) that uses a right clicked event within selected range of cells on Active Sheet and only if workbook is named “Master”. It allows me add new comments/data to dynamic validation range for that selected cell. Everything works great for entering new comments to existing list. What I would like to do is add an option to remove old comments from list so as not to clutter up with too many not used comments. The reason for this approach instead of just going to list location and just deleting is that the worksheet that holds list is hidden and is unavailable to user.

Code: Select all

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    If ActiveWorkbook.Name <> "Master.xlsm" Then
        Exit Sub
    End If

    ' Appends data entry to dynamic validation range on right click
    ' The validation range must have nothing below
    If Not Intersect(Range("E18,E32,E46,E60,E74,E89"), Target) Is Nothing Then
        Cancel = True    ' Don't perform the standard action
        ' Your code here; Target is the cell being double-clicked

        Dim inter As Range    ' a cell with validation, maybe
        Dim cell As Range
        Dim r As Range    ' validation range
        Dim sVal As String    ' list validation formula
        Dim Answer As String
        Dim MyNote As String


        'If Target.Count > 1 Then Exit Sub    '------------------------------------->
        'Place your text here
        MyNote = "Select Yes to add new comment or No to cancel selection."

        'Display MessageBox
        Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Adding comment")

        If Answer = vbNo Then
            'Code for No button Press
            MsgBox "You have selected No, your selection has been cancelled."
            Exit Sub
        Else
            'Code for Yes button Press
            MsgBox "Your new comment has now been added."
        End If

        On Error Resume Next
        Set inter = Intersect(Target, Cells.SpecialCells(xlCellTypeAllValidation))
        If inter Is Nothing Then Exit Sub    ' ------------------------------------>
        For Each cell In inter
            If cell.Validation.Type <> xlValidateList Then Exit Sub    '------------->

            sVal = cell.Validation.Formula1
            If Left(sVal, 1) <> "=" Then Exit Sub   ' ------------------------------->

            Set r = ThisWorkbook.Names(Mid(sVal, 2)).RefersToRange
            If r Is Nothing Then Exit Sub   ' --------------------------------------->
            If IsNumeric(Application.Match(cell.Text, r, 0)) Then Exit Sub  '------>

            Cancel = True
            With r
                .Parent.Cells(Me.Rows.Count, .Column).End(xlUp)(2).Value = cell.Text
                .Resize(.Count + 1).Sort _
                        Key1:=r(1), Order1:=xlAscending, _
                        MatchCase:=False, Orientation:=xlTopToBottom, Header:=xlNo
            End With
        Next cell
        Beep    ' the sound of success
    End If
End Sub

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

You could provide a userform with a list box that lists the current options, and a command button that removes the selected item from the hidden sheet.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Hi Hans
I have been trying to figure out how to use a userform that will let me select either Add or Remove with the use of command buttons. I left the right click event in place for targeted cells that would then open the userform. I then tried applying the remaining code, less the message box. I tried making some changes in reference to selected cell. Of course this is just to add comment; I haven’t even begun trying to figure out the removal part YIKES!!! :dizzy:

I added a sample workbook below
Add or Remove Comment.xlsm
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

I'll get back to you but it'll take a while to set up the whole thing.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Thank You Hans
Your help is always greatly appreciated and without a doubt worth the wait…. :grin:

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

It turned out to be easier than I thought. Try the attached version - test thoroughly, please!
Add or Remove Comment2.xlsm
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Thank you Hans
Everything seems to works fantastic with all my dynamic validation ranges on the comment sheet, all I had to do was drop Userform into a copy of master workbook (just to play it safe) add a little piece code, a little of this and a little of that and presto. :grin:
Many thanks

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Hi Hans
I thank you for this code and it does work very well, but due to the large amount of dynamic validation ranges that I use it can get a little difficult to decipher which dynamic validation ranges goes where. I was wondering if this code could be modified so if I were to first select a cell containing dynamic validation range then select a command button it would then show me only those comments for that selected range using a userform as before with a selected Delete button. Also I have a separate piece of code that assist me with populating my dynamic validation range with new comment that I would like to add to same userform to simplify add or delete comment from dynamic validation range.

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

Change the UserForm_Initialize code for frmDelete to

Code: Select all

Private Sub UserForm_Initialize()
  Dim wsh As Worksheet
  Dim c As Long
  Dim n As Long
  Dim strValidation As String
  Set wsh = Worksheets("Comments")
  n = wsh.Cells(1, wsh.Columns.Count).End(xlToLeft).Column
  For c = 1 To n
    Me.cboCategories.AddItem wsh.Cells(1, c)
  Next c
  On Error Resume Next
  strValidation = ActiveCell.Validation.Formula1
  If strValidation <> "" Then
    strValidation = Mid(strValidation, 2)
    Me.cboCategories = strValidation
  Else
    Me.cboCategories.ListIndex = 0
  End If
End Sub
If the active cell has data validation with a list, that list automatically displayed.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Thank you Hans
Works great, this will help make deletion so much easier.
As always your assistance is deeply and greatly appreciated
:grin:

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Hans as I was trying to consolidate both codes on a single userform that has two command buttons one to add new comment and the other to remove. The new code you provided to remove works great, but when I tried adding the code I had previously to add comment (Code located on first comment of this thread) I’m getting an error message.
What I tried to do was to add only top part of the code to the worksheet then added code that would that would then open a new userform named AddRemovCom. As seen below

Code: Select all

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    If ActiveWorkbook.Name <> "Add or Remove Comment.xlsm" Then
        Exit Sub
    End If

    ' Appends data entry to dynamic validation range on right click
    ' The validation range must have nothing below
    If Not Intersect(Range("E18,E32,E46,E60,E74,E89,E104,E119,E134,H20,H34,H48,H62,H76,H91"), Target) Is Nothing Then
        Cancel = True    ' Don't perform the standard action
        ' Your code here; Target is the cell being double-clicked

        AddRemovCom.Show
    End If
End Sub
Then thinking I could add the remaining code, not so

Code: Select all

        Dim inter As Range    ' a cell with validation, maybe
        Dim cell As Range
        Dim r As Range    ' validation range
        Dim sVal As String    ' list validation formula
        Dim Answer As String
        Dim MyNote As String


        'If Target.Count > 1 Then Exit Sub    '------------------------------------->
        'Place your text here
        MyNote = "Select Yes to add new comment or No to cancel selection."

        'Display MessageBox
        Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Adding comment")

        If Answer = vbNo Then
            'Code for No button Press
            MsgBox "You have selected No, your selection has been cancelled."
            Exit Sub
        Else
            'Code for Yes button Press
            MsgBox "Your new comment has now been added."
        End If

        On Error Resume Next
        Set inter = Intersect(Target, Cells.SpecialCells(xlCellTypeAllValidation))
        If inter Is Nothing Then Exit Sub    ' ------------------------------------>
        For Each cell In inter
            If cell.Validation.Type <> xlValidateList Then Exit Sub    '------------->

            sVal = cell.Validation.Formula1
            If Left(sVal, 1) <> "=" Then Exit Sub   ' ------------------------------->

            Set r = ThisWorkbook.Names(Mid(sVal, 2)).RefersToRange
            If r Is Nothing Then Exit Sub   ' --------------------------------------->
            If IsNumeric(Application.Match(cell.Text, r, 0)) Then Exit Sub  '------>

            Cancel = True
            With r
                .Parent.Cells(Me.Rows.Count, .Column).End(xlUp)(2).Value = cell.Text
                .Resize(.Count + 1).Sort _
                        Key1:=r(1), Order1:=xlAscending, _
                        MatchCase:=False, Orientation:=xlTopToBottom, Header:=xlNo
            End With
        Next cell
        Beep    ' the sound of success
    End If
End Sub

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

You can't just merge two fragments of code and expect it to work. I think your code is becoming too complicated; I'd stick with the separate form.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Hi Hans
I used your suggestion, and used the delete process separately. This approach does work quite well. What I was thinking of doing was to eliminate the dropdown that will help with possible confusion of accidently deleting wrong comment from wrong list. I went to dropdown cboCategories properties on userform frmDelete, then on the selection of ShowDropButtonWhen I changes to Never, so that it will only show name list from selected cell. This seems to work OK except if no name list is selected it then resorts to first name list. Is it possible that if no dynamic name list is selected a message could appear.

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

You could change the code that opens frmDelete:

Code: Select all

Private Sub CommandButton2_Click()
  Dim strFormula As String
  On Error Resume Next
  strFormula = ActiveCell.Validation.Formula1
  On Error GoTo 0
  If strFormula = "" Then
    MsgBox "The active cell doesn't have validation.", vbExclamation
  Else
    frmDelete.Show
  End If
End Sub
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Thank you Hans :cheers:

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Hi Hans
I have been using this code that you have provided with great success, until this morning. What I noticed was when selecting a dynamic validation range and then open Userform frmDelete as if to delete a comment everything works great except if I where to delete the very first selection, it would then cause the dynamic validation range to fail upon further use. I can delete any comment on the Userform list except for the very first one.
Here’s a workbook with the current changes mentioned in this thread.
Remove Comment.xlsm
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78485
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Code to remove comment from dynamic validation range

Post by HansV »

A name such as MainComments is defined as

=OFFSET(Comments!$A$2,0,0,COUNTA(Comments!$A$2:$A$200),1)

By deleting A2, the reference to Comments!$A$2 becomes invalid. You can work around this by changing the definition of MainComments to

=OFFSET(Comments!$A$1,1,0,COUNTA(Comments!$A$2:$A$200),1)

Unfortunately, you will have to do this for all these ranges.
Best wishes,
Hans

ABabeNChrist
SilverLounger
Posts: 1868
Joined: 25 Jan 2010, 14:00
Location: Conroe, Texas

Re: Code to remove comment from dynamic validation range

Post by ABabeNChrist »

Thank you Hans
I’ll go through the Name Manager one by one and make needed changes, and yes there are quite a few, No biggie though, makes for good practice and a test of patience. :notmyfault: