tags:

views:

79

answers:

2

i wrote the following section below. when debugging, i see that i enter the first Case okay. my problem is with the second Case - it does not enter it and goes to the error messege.

what do i do wrong?

Select Case Data_Rate
  Case "1", "2", "5.5", "11", "6", "9", "12", "18", "24", "36", "48", "54"
    a = Data_Rate

    Select Case Date_Rate
      Case "1"
        b = 2
      Case "2", "5.5", "11"
        b = 1
      Case Else:
        MsgBox ("ERROR - Data_Rate")
    End Select

  Case "0", "1", "2", "3", "4", "5", "6", "7"
    a = 3
  Case Else:
    MsgBox ("ERROR - Data_Rate")
End Select
A: 

For values like 1, 2, and 6 that are in both, it will always only execute the first case

I case statement is like a bunch of if/else if statements.

Only one block is executed

Not multiple blocks

Chad
but i cannot have several values in "if" statement like in a "case" statement, right?can i have something like: if (Date_Rate = "1" or "2" or "11") Then .... ???
Nimrod
never mind - the "IF" works :)thanks!
Nimrod
@Nimrod, yes, just it will be written as if (Date_Rate = "1" or Date_Rate = "2" or Date_Rate = "11")
Chad
+2  A: 

Is that your actual code pasted in above?

If so, it may be because you have "datE_rate" instead of "datA_rate" as the value for your nested Select statement.

If not, or even if so, you may also want to differentiate your error messages so it's clearer whether it's the inner or outer Select that is failing.

Edit:

Also, I don't think you are using Option Explicit. If you were, it would have caught your mistaken use of Date_Rate, unless you actually do have such a variable.

Todd
+1 for not being blind ;)
Georg Fritzsche
Thanks Todd.I do not know if that was the problem since i changed the code to IF/Else already.however, i do not have any "date_rate" variable - and i had no warning message...is there a VBA configuration option i need to set in order to have this error pop-up?you mentioned something about "Option Explicit"??
Nimrod
You need to manually add Option Explicit to the top of your existing modules, then go into Tools> Options and check "Require Variable Declaration" on the first tab (Editor). Now any new modules you create will have Option Explicit already inserted at the top. You will then need to Dim every variable that you use. It's worth it in saving time tracking down mysterious bugs like this. Read up on it on MSDN and other websites, like this for example: Using Option Explicit(http://www.4guysfromrolla.com/webtech/faq/Intermediate/faq6.shtml). This is about ASP (VBScript), but it applies to VBA too.
Todd