Reputation: 1473
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
Upvotes: 1
Views: 40344
Reputation: 50034
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.
Upvotes: 2