Select Case Code . .

Evagrius Ponticus

Well-known Member
Joined
May 24, 2007
Messages
1,467
Hi All,

I am not sure why this isn't working. When I step through the target and the range "Ratio" are equal but the code skips the line that selects the sheet. Thank you for any help.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("MyRange")) Is Nothing Then
Select Case Target
    Case Target.Address = Range("Ratio").Address
        Sheet1.Select
    End Select
End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
You cannot use a condition in a Case statement...

Either you need to do this:
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("MyRange")) Is Nothing Then
Select Case Target.Address
    Case Range("Ratio").Address
        Sheet1.Select
    End Select
End If
End Sub
or you need to rewrite it as a simple If Then
 
Upvote 0
Purely syntactical, your code was okay... but just for the sake of, let's explain why it didn't work :biggrin:

Select Case Target
this says you want to make a decision based on the value of Target
Case Target.Address = Range("Ratio").Address
this says you want to compare the Target value to the expression Target.Address = Range("Ratio").Address
That expression may result in true or false... but neither of those will ever be equal to target (a range), so the case is not fulfilled and the code in it is skipped.

So, when I said you cannot use a condition in a case statement, I really meant: it seldom makes any sense to use a condition in a case statement :biggrin:
 
Upvote 0
Ahh - I see now. So what I wrote was generating a boolean - therefore a boolean will not equal a "range", right? But is it acceptable to use "IF" statements within a Select Case? Thank you!!
 
Upvote 0
Exactly, you tried to compare a boolean to a range.

You can use any code you like within the Select Case, also If Then, but if you think you need to, you might want to search for a better way to express your intent.

Simple decisions can be done with If Then ElseIf Else Endif, nested if necessary. When you have a lot of if then statements on the same condition, you can better use the Select Case construction.
For each case within the Select Case, you can create a whole block of code, with more If Then, or even another Select Case structure...

What you cannot do is have those constructions as a value for a Case. But the code in a case 'block' can of course contain all valid VB syntax...

I hope I made things clearer with that explanation :biggrin:
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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