Assistance needed with VBA code please

H100K

New Member
Joined
Mar 29, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I am currently using this VBA code below however, it means that all the cells change color but I only need it to apply to these range of cells E5:E26, F5:F26, J5:J22 and K5:K22 can anybody help with this?

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case Else: Target.Interior.ColorIndex = 4
End Select
End Sub

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = xlNone
End Sub

Thankyou!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the Board!

Try something like this:
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    Dim rng As Range
    
    Set rng = Range("E25:F26, J5:K22")
    
    If Not (Intersect(Target, rng) Is Nothing) Then
        Cancel = True
        Select Case Target.Interior.ColorIndex
            Case xlNone, 4: Target.Interior.ColorIndex = 3
            Case Else: Target.Interior.ColorIndex = 4
        End Select
     End If
     
End Sub
 
Upvote 1
Solution
Hello Joe4,

Thankyou for the warm welcome!

This works great just one thing if I wanted the option to change back to white could this be added in the code please?

Thanks again!
 
Upvote 0
Hello Joe4,

Thankyou for the warm welcome!

This works great just one thing if I wanted the option to change back to white could this be added in the code please?

Thanks again!
If you mean from your second procedure, you should be able to apply the exact same logic to that that I used in the first one.
Give it a try and see how it works! It will be a good test to see how well you understand the method I used.
Write back if you have questions.
 
Upvote 0
Thankyou Joe,

Ended up tweaking yours slightly and it works fine!

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

Dim rng As Range

Set rng = Range("E5:F26, J5:K22")

If Not (Intersect(Target, rng) Is Nothing) Then
Cancel = True
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case Else: Target.Interior.ColorIndex = 4
End Select
End If

End Sub


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

Dim rng As Range

Set rng = Range("E5:F26, J5:K22")

If Not (Intersect(Target, rng) Is Nothing) Then
Cancel = True
Select Case Target.Interior.ColorIndex
Case 4, xlNone: Target.Interior.ColorIndex = xlNone
Case Else: Target.Interior.ColorIndex = xlNone
End Select
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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