1

I'm trying to write an "If" statement that checks to see if a the Key that corresponds to a Player object in a (Scripting.)Dictionary called Players has a String value that contains the letter "P."

I must be missing some VBA syntactical sugar, but I can't seem to find the answer to what I'm doing wrong anywhere.

My statement right now is:

For Each Key In Players.Keys()
    If Not (Players(Key).Position Like "*P*") Then 'something'
    End If
Next Key

However, it selects the first dictionary entry it loops through even though the Position property has a P in it.

In this case Player(Key).Position = "RP," which I would like to then skip over the "Then" statement. It currently does not work.

Any help would be appreciated.

Edit

The Player class is:

'Class Module: Player
Public Name As String
Public Position As String
Public WAR As Double
Magnetron
  • 7,495
  • 1
  • 25
  • 41
obizues
  • 1,473
  • 5
  • 16
  • 30
  • I believe you just want to test `Players(Key)` which will return the value for that key in the `Players` dictionary. Instead of testing `Players(Key).Position`. I'm not sure what ".Position" would return here. That's not a property or method of a dictionary object. – JNevill May 03 '18 at 19:56
  • You could, also, just loop through the values of the dictionary instead of the keys with `For Each player in Players.Items`... I'm not sure what you are doing though so maybe that's not an option. – JNevill May 03 '18 at 19:58
  • @JNevill I edited my original post to include the player class. If I just use Players(Key) it returns a Player Object, which is not the Position attribute that is a string, which I want to check if that contains a P. – obizues May 03 '18 at 19:59
  • Ah! Dictionary item is an object. Mind=blown. That makes sense now. What value do you get if you toss a breakpoint on that IF test for `Players(key).Position`. Something seems off there as your If condition is correct. – JNevill May 03 '18 at 20:04
  • @JNevill "RP" - which is the most confusing part – obizues May 03 '18 at 20:05
  • I've added an answer, that isn't much of an answer, but I just quickly built this out to test and couldn't replicate. I suspect something else is funky in your code before this test.. I can't imagine what though. – JNevill May 03 '18 at 20:21
  • @JNevill to further confuse this.. based on the debugger "Players(Key).Position Like "*P*"" = true and "Not Players(Key).Position Like "*P*"" = false – obizues May 03 '18 at 20:29
  • You can toss backticks around your code in comments to preserve the code and format it nicely. I'm assuming you have asterisks in that LIKE condition, right? – JNevill May 03 '18 at 20:31
  • I agree with @JNevill in that I cannot reproduce your problem here. – Ron Rosenfeld May 03 '18 at 21:13

1 Answers1

2

This doesn't solve your problem, but... it may help. I was not able to reproduce this behavior:

Player Class Module:

'Class Module: Player
Public Name As String
Public Position As String
Public WAR As Double

Subroutine:

Sub test()
    Dim players As Scripting.Dictionary
    Set players = New Scripting.Dictionary

    Dim pers As Player

    Set pers = New Player
    pers.Position = "RP"
    players.Add "1", pers

    Set pers = New Player
    pers.Position = "What"
    players.Add "2", pers

    Set pers = Nothing

    For Each pkey In players.Keys
        If Not (players(pkey).Position Like "*P*") Then
            Debug.Print players(pkey).Position, "Not a P Player"
        Else
            Debug.Print players(pkey).Position, "Its a P Player"
        End If
    Next

End Sub

Results in Immediate Pane:

RP            Its a P Player
What          Not a P Player

Like I said in the comments, I don't know why this isn't working for you, but hopefully seeing this in simplified code may point out some problem with your class implementation, or your dictionary iterations, or your like condition... or something that isn't obvious in the bit of code you have shared.

JNevill
  • 46,980
  • 4
  • 38
  • 63