VBA - Combining two Private Subs

Jasesair

Active Member
Joined
Apr 8, 2015
Messages
282
Office Version
  1. 2016
I'm still learning VBA, so any help here would be appreciated. I have two Worksheet Changes below that I understand will only work if combined. They have different ranges with different colours. Basically, it's do one or the other depending on the cell selection. Can anyone assist to join these two?

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Dim rng As Range
Cancel = True
Set rng = ActiveSheet.Range("f9:f18")
Worksheets("Management").Unprotect
If Intersect(Target, rng) Is Nothing Then Exit Sub
Select Case True
    Case Target.Interior.Color = RGB(255, 255, 255): Target.Interior.Color = RGB(255, 199, 206)
    Case Target.Interior.Color = RGB(255, 199, 206): Target.Interior.Color = RGB(255, 255, 255)
End Select

Worksheets("Management").Protect
End Sub

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

Dim rng As Range
Cancel = True
Set rng = ActiveSheet.Range("g9:k18")
Worksheets("Management").Unprotect
If Intersect(Target, rng) Is Nothing Then Exit Sub
Select Case True
    Case Target.Interior.Color = RGB(255, 255, 255): Target.Interior.Color = RGB(198, 239, 206)
    Case Target.Interior.Color = RGB(198, 239, 206): Target.Interior.Color = RGB(255, 255, 255)

End Select
Worksheets("Management").Protect
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

   Cancel = True
   Me.Unprotect
   If Not Intersect(Target, Range("F9:F18")) Is Nothing Then
      Select Case True
          Case Target.Interior.Color = RGB(255, 255, 255): Target.Interior.Color = RGB(255, 199, 206)
          Case Target.Interior.Color = RGB(255, 199, 206): Target.Interior.Color = RGB(255, 255, 255)
      End Select
   ElseIf Not Intersect(Target, Range("G9:K18")) Is Nothing Then
      Select Case True
          Case Target.Interior.Color = RGB(255, 255, 255): Target.Interior.Color = RGB(198, 239, 206)
          Case Target.Interior.Color = RGB(198, 239, 206): Target.Interior.Color = RGB(255, 255, 255)
      
      End Select
   End If
   Me.Protect
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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