PDA

View Full Version : Solved: Array problems



wnazzaro
06-29-2007, 08:53 AM
I'm missing something and I don't know what.

Public Sub MyTest()
Dim Val
Val = Array(38, 36, 26, 62, 10, 28, 15, 7, 78)
Dim myClass(8)

For i = 0 To 8 Step 3
Select Case Val(i)
Case Val(i) >= 75
myClass(i) = "Good"
myClass(i + 1) = "Good"
myClass(i + 2) = "Good"
Case Val(i) >= 50
myClass(i) = "Med"
myClass(i + 1) = "Med"
myClass(i + 2) = "Med"
Case Else
myClass(i) = "Bad"
myClass(i + 1) = "Bad"
myClass(i + 2) = "Bad"
End Select
Next

End Sub
For some reason myClass(3) comes up as "Bad" however I expect "Med". myClass(6) should be "Good" but still comes up "Bad". Help.
:banghead:

TrippyTom
06-29-2007, 09:13 AM
The value of (i) is only going to be 0, 3, or 6 during the loop, so it's always going to run the ELSE case.

Norie
06-29-2007, 09:31 AM
I don't think it's a good idea to be using Val for the array name, that's going to clash with the Val function and could be the root of your problem.

rbrhodes
06-29-2007, 01:53 PM
Hi I've changed Val to Valu as suggested by Norie (but that's not the problem) The actiual problem is in the syntax of the Select/Case statement.

Case Is => 50 is proper usage

State what to check then check it.



Public Sub MyTest()

Dim Valu()
Dim myClass(8)

Valu = Array(38, 36, 26, 62, 10, 28, 15, 7, 78)

For i = 0 To 8 Step 3
'here you tell it what to check: Valu(i)
Select Case Valu(i)
'here you check it
Case Is >= 75
myClass(i) = "Good"
myClass(i + 1) = "Good"
myClass(i + 2) = "Good"
'here you check it
Case Is > 50
myClass(i) = "Med"
myClass(i + 1) = "Med"
myClass(i + 2) = "Med"
Case Else
myClass(i) = "Bad"
myClass(i + 1) = "Bad"
myClass(i + 2) = "Bad"
End Select
Next

End Sub



Cheers,

dr

Norie
06-30-2007, 07:04 AM
I agree the syntax of the Select Case is incorrect but that's not the only problem.

The use of Val will only ever return 0,3 or 6 as TrippyTom pointed out.

wnazzaro
07-01-2007, 06:30 AM
I agree the syntax of the Select Case is incorrect but that's not the only problem.

The use of Val will only ever return 0,3 or 6 as TrippyTom pointed out.

No, the Dim statement overrides the Val() function. If you test this in Excel, Val(3) returns 62 because Val() is an array. When writing code the prompt is now "Local Val() as Variant" instead of "Val (String As String) As Double". I do agree that it is not a good idea to do this, but that is not the problem. The problem was with the Select Statement. However since I was trying to do this in VBScript the question is moot. Select statements in VBScript can't evaluate greater than or less than (darn scripting language) so I used an If...Elseif...

Again, thanks for the help.

Bill

Norie
07-01-2007, 07:13 AM
Bill

That very well maybe the case but I still wouldn't recommend using the names of VBA or VBScript functions for variables.

I know in VBScript you can only use the Select Case to check for values, but if you were actually using VBA what rbrhodes posted should work.

In your code you were comparing the values in the array to the results of the comparison, which would turn a boolean value.

eg Val(i) >= 75 will return TRUE/FALSE