Double click => returns different message boxes depending on cell color?

Ayra88

New Member
Joined
Nov 23, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Dear friends,

I'm quite new to VBA and have been confronted with a difficult task at my job - I have been struggling with this for some hours, I hope you could help me. I'll try to make the problem simple:

Say, I have in column A various projects with different colors representing the risk of the projects.

I want to write a VBA Code that will perform the following:

If I double click on a green project => A message box should appear containing the text "Low Risk"
If I double click on a yellow project => A message box should appear containing the text "Medium Risk"
If I double click on an orange project => A message box should appear containing the text "High Risk"

I have formatted the cells with pastel colors, in the column C we can see the color codes of the cells.

1606167751229.png


Please see my code below. The Problem is, it doesn't matter on which cell I click, only the message box "Low Risk" will appear. I don't quite get where is the mistake.

I would very much appreciate your help!

(In reality the problem would be: double click on green cell => jump to another sheet ( I already have this part written); double click on yellow cell => message box "...."; double click on orange cell =>message box "...")

Best Regards, A.

---

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As rangE, Cancel As Boolean)
Dim Cell As range
For Each Cell In range("A1:A15")
With Cell
If Not Intersect(Target, range("A1:A15")) Is Nothing Then
If Cell.Interior.Color = 11854022 Then
Call showGreen
Exit For
ElseIf Cell.Interior.Color = 10092543 Then
Call showyellow
Exit For
ElseIf Cell.Interior.Color = 11389944 Then
Call showRed
Exit For
End If
End If
End With

Next Cell
End Sub
---
Sub showGreen()
Dim showCheck
showCheck = MsgBox("Low Risk")
End Sub
---
Sub showyellow()
Dim showCheck
showCheck = MsgBox("Medium Risk")
End Sub
---
Sub showRed()
Dim showCheck
showCheck = MsgBox("High Risk")
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Not Intersect(Target, Range("A1:A15")) Is Nothing Then
      If Target.Interior.Color = 11854022 Then
         Call showGreen
      ElseIf Target.Interior.Color = 10092543 Then
         Call showyellow
      ElseIf Target.Interior.Color = 11389944 Then
         Call showRed
      End If
      Cancel = True
   End If
End Sub
 
Upvote 0
Solution
Dear Fluff,

wow it works perfect! Thank you very very much! May I ask what does the line "Cancel = True" mean?
 
Upvote 0
It prevents the cell from going into edit mode.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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