View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Brettjg Brettjg is offline
external usenet poster
 
Posts: 295
Default Select case question

That's probably a good Q Jacob. Immediatelt before the select is:

If Range("S1").Offset(0, 1) 0 Then: m1L = Range("S1").Offset(0,
1).Value: lp_cnt = lp_cnt + 1
If Range("S2").Offset(0, 1) 0 Then: m2L = Range("S2").Offset(0,
1).Value: lp_cnt = lp_cnt + 1
If Range("S3").Offset(0, 1) 0 Then: m3L = Range("S3").Offset(0,
1).Value: lp_cnt = lp_cnt + 1
If Range("S4").Offset(0, 1) 0 Then: m4L = Range("S4").Offset(0,
1).Value: lp_cnt = lp_cnt + 1
If Range("S5").Offset(0, 1) 0 Then: m5L = Range("S5").Offset(0,
1).Value: lp_cnt = lp_cnt + 1

S1 S2 etc aren't the real names (just to shorten the code in this post). I'm
setting the value of lp_cnt which in turn will set whether I look for (m1S)
or (m1S and m2S) etc. There may be a better way to express this
but...............
"DO SOME STUFF" is identical each time through the 5 selects.
Regards, Brett

"Jacob Skaria" wrote:

If "Do some Stuff" is same; the only change is lp_cnt and Lsh.Cells(lk_rw,
lk_cl).Value. I am just trying to understand why m1L is mentioned in all
cases.

If this post helps click Yes
---------------
Jacob Skaria


"Brettjg" wrote:

I'd like to compress the following code down to just the one Select block.
The only difference between the two is that in the second I'm testing for two
conditions. There are actually five of these altogether, so the code gets
quite lengthy as a result of the "DO SOME STUFF" part (which doesn't change).
The range that I'm selecting doesn't change either - only the lp_cnt and the
number of tests changes m1L, m2L etc).

If lp_cnt = 1 Then
Select Case Lsh.Cells(lk_rw, lk_cl).Value
Case m1L
"DO SOME STUFF"
End Select
End If

If lp_cnt = 2 Then
Select Case Lsh.Cells(lk_rw, lk_cl)
Case m1L, m2L
"DO SOME STUFF"
End Select
End If

Is there a way to set the case test for a variable amount of tests i.e. (and
I know this wouldn't work because I can't have a statement between Select and
Case)
If lp_cnt = 1 Then: Case m1L
If lp_cnt = 2 Then: Case m1L, m2L
If lp_cnt = 3 Then: Case m1L, m2L, m3L
If lp_cnt = 4 Then: Case m1L, m2L, m3L, m4L
If lp_cnt = 5 Then: Case m1L, m2L, m3L, m4L, m5L

Regards, Brett