Consulting

Results 1 to 7 of 7

Thread: Array problems

  1. #1
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location

    Array problems

    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.
    Last edited by Aussiebear; 04-21-2023 at 07:04 PM. Reason: Adjusted the code tags

  2. #2
    VBAX Expert TrippyTom's Avatar
    Joined
    Jul 2005
    Location
    New York, NY (USA)
    Posts
    556
    Location
    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.
    Office 2010, Windows 7
    goal: to learn the most efficient way

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  4. #4
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    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
    Last edited by Aussiebear; 04-21-2023 at 07:07 PM. Reason: Adjusted the code tags

  5. #5
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Feb 2005
    Posts
    82
    Location
    Quote Originally Posted by Norie
    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

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •