我试图写一个" If"检查一个名为Players的(Scripting。)字典中与Player对象相对应的Key是否具有包含字母" P的字符串值的语句。"
我必须错过一些VBA语法糖,但我似乎无法找到我在任何地方做错的答案。
我现在的发言是:
For Each Key In Players.Keys()
If Not (Players(Key).Position Like "*P*") Then 'something'
End If
Next Key
但是,即使Position属性中有P,它也会选择它循环的第一个字典条目。
在这种情况下,玩家(键).Position =" RP,"然后,我想跳过"然后"声明。它目前无效。
任何帮助都将不胜感激。
修改
Player类是:
'Class Module: Player
Public Name As String
Public Position As String
Public WAR As Double
答案 0 :(得分:1)
这不能解决您的问题,但......可能有所帮助。我无法重现这种行为:
玩家类模块:
'Class Module: Player
Public Name As String
Public Position As String
Public WAR As Double
子程序:
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
即时窗格中的结果:
RP Its a P Player
What Not a P Player
就像我在评论中所说,我不知道为什么这对你不起作用,但希望在简化代码中看到这一点可能会指出你的类实现,字典迭代或类似情况的一些问题......或者你所分享的代码中不明显的东西。