VBA for adding and removing a name from a named range (I think I'm halfway there)

grf

Board Regular
Joined
Oct 30, 2004
Messages
70
Office Version
  1. 365
Hi Experts,
On my DATA Sheet, clients’ names are listed on Rows 3-52 on Column B. The Double Click macro below allows me to copy a Client’s name from column B and add it to the bottom of a Dynamic Named Range entitled “cashdd” which starts on Row 87 of Column A and records the transaction with a tick in the relevant cell in Column G. At present a Double Click a on a ticked cell in G Rows 3-52 will remove the tick and I wondered if there was any way of removing the name from “cashdd” as well
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Not Intersect(Target, Range("G3:G52")) Is Nothing Then
Cancel = True
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"

If Target = "a" Then Target.Offset(, -5).Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
Else
Target = vbNullString

End If
End If
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("G3:G52")) Is Nothing Then
Cancel = True
On Error GoTo M
Dim SearchString As String
Dim SearchRange As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"
    If Target = "a" Then Target.Offset(, -5).Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
        Else
        Target = vbNullString
        SearchString = Target.Offset(, -5).Value
        Set SearchRange = Range("A1:A" & Lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
        SearchRange.Value = ""
    End If
End If
Exit Sub
M: MsgBox "No value found"
End Sub
 
Upvote 0
Or this:
Previous script cleared the value in column "A" when check was cleared in column "G"
This script will delete the cell and shift up so no empty cells.
Use which ever is best for you:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("G3:G52")) Is Nothing Then
'Modified 7-22-17 3:00 PM EDT
Cancel = True
On Error GoTo M
Dim SearchString As String
Dim SearchRange As Range
Dim Lastrow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"
    If Target = "a" Then Target.Offset(, -5).Copy Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1)
        Else
        Target = vbNullString
        SearchString = Target.Offset(, -5).Value
        Set SearchRange = Range("A1:A" & Lastrow).Find(SearchString, LookIn:=xlValues, lookat:=xlWhole)
        SearchRange.Delete xlShiftUp
    End If
End If
Exit Sub
M: MsgBox "No value found"
End Sub
 
Upvote 0
My Aswer Is This,
I thought this was going to be so difficult that I was reluctant to ask about moving the list up to avoid gaps. That was going to be my next question. Well, not only did your solution work first time straight out of the box but you have anticipated my next question and nailed that too. Thank you very much My Aswer Is This, I’m very grateful, I didn’t know about SearchString and SearchRange and would never have been able to achieve this on my own. Thanks again,
Regards grf
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
My Aswer Is This,
I thought this was going to be so difficult that I was reluctant to ask about moving the list up to avoid gaps. That was going to be my next question. Well, not only did your solution work first time straight out of the box but you have anticipated my next question and nailed that too. Thank you very much My Aswer Is This, I’m very grateful, I didn’t know about SearchString and SearchRange and would never have been able to achieve this on my own. Thanks again,
Regards grf
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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