How to create a VBA code to delete a private sub on sheet1?

The below code works great, but I want the user to have a choice whether or not to keep it.  I’m trying to create a code that I can assign to a button that will delete the below code if the user does not want the option.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
 ' This subroutine colors a cell red when double-clicked then clears it when double-clicked again.
 ' Some values for .ColorIndex are...
 ' Red = 3, Green = 4, Blue = 5, Yellow = 6, Orange = 45
 ' Google "VBA color palette" for more colors
  If Intersect(Target, Range("B3:B10")) Is Nothing Then Exit Sub
     ' If the cell is clear
     If Target.Interior.ColorIndex = xlNone Then
  
         ' Then change the background color to yellow
         Target.Interior.ColorIndex = 27
 
     ' Else if the cell background color is yellow
     ElseIf Target.Interior.ColorIndex = 27 Then
  
         ' Then clear the background
         Target.Interior.ColorIndex = xlNone
  
     End If
  
     ' This is to prevent the cell from being edited when double-clicked
     'Cancel = True
  
 End Sub

Open in new window

kbay808Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Instead of deleting the code, add as global variable like bSkip and then in a sub ask the user if he wants to skip the code. If he says yes then set bSkip to True. You would need to modify your code above to look like this:


Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
 ' This subroutine colors a cell red when double-clicked then clears it when double-clicked again.
 ' Some values for .ColorIndex are...
 ' Red = 3, Green = 4, Blue = 5, Yellow = 6, Orange = 45
 ' Google "VBA color palette" for more colors

If bSkip Then Exit Sub
  If Intersect(Target, Range("B3:B10")) Is Nothing Then Exit Sub
     ' If the cell is clear
     If Target.Interior.ColorIndex = xlNone Then
  
         ' Then change the background color to yellow
         Target.Interior.ColorIndex = 27

...Etc

Open in new window

0
kbay808Author Commented:
I'm not sure how to create a code to set the global variable.  I created a dropdown box in cell "H1" with a True and False option.  Here was my attempt to create the code to set the variable.  I placed it in the Sheet code.  Needless to say, it does not work.

 Private Sub Workboot_bSkip()
 Public bSkip As Integer
 bSkip = Range("H1")
 End Sub

Open in new window

0
Martin LissOlder than dirtCommented:
Option Explicit 
Private bSkip As Boolean

Private Sub Workboot_bSkip()
 bSkip = Range("H1")
End Sub

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Martin LissOlder than dirtCommented:
Note that the above assumes that you have "Option Explicit" at the top of your sheet's code. Having it there is not absolutely required but it's a good idea that we can discuss if you don't know what it does. In any case line 2 should either be under it or at the top of the sheet if Option Explicit isn't there.
0
Roy CoxGroup Finance ManagerCommented:
You can also ask the user if he wants to continue

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
' This subroutine colors a cell red when double-clicked then clears it when double-clicked again.
' Some values for .ColorIndex are...
' Red = 3, Green = 4, Blue = 5, Yellow = 6, Orange = 45
' Google "VBA color palette" for more colors
    If Intersect(Target, Range("B3:B10")) Is Nothing Then Exit Sub
    Select Case _
           MsgBox("This action will add a background colour to the selected cell. Do you want to continue", _
                  vbOKCancel Or vbQuestion Or vbDefaultButton1, "Continue?")

    Case vbOK
        ' If the cell is clear
        If Target.Interior.ColorIndex = xlNone Then

            ' Then change the background color to yellow
            Target.Interior.ColorIndex = 27

            ' Else if the cell background color is yellow
        ElseIf Target.Interior.ColorIndex = 27 Then

            ' Then clear the background
            Target.Interior.ColorIndex = xlNone

        End If

        ' This is to prevent the cell from being edited when double-clicked
        'Cancel = True
    Case vbCancel

    End Select


End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kbay808Author Commented:
You put me on the right track.  The below code works as a trigger.

  If Sheets("HPSM").Range("G2").Value = "Off" Then
Exit Sub
Else
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.