我有大約 450 個元素的腳本字典。我需要在索引> 256 的間諜視窗元素中進行除錯,但 Excel VBA 將視圖限制為索引 256。我要除錯的是缺少一些鍵并跟蹤應該創建它們時發生的情況。
這是宣告:Set col = CreateObject("Scripting.Dictionary")
我希望能夠在 Python 中撰寫類似的東西:col[256:512]
作為一種解決方法,我可以在除錯終端中列印所有鍵,如下所述:For Each c In col: Debug.Print c: Next
巴士有辦法在監視/間諜視窗中做我想做的事嗎?
uj5u.com熱心網友回復:
添加 watch -> col.Itemsorcol.keys作為運算式。
uj5u.com熱心網友回復:
列印一些字典鍵和專案
后期裝訂
- 使用后期系結時,不能使用“索引功能”(
dict.Keys(n),dict.Items(n))。所以你只能回圈升序,你可以使用下面的方法。
Sub PrintDictLate( _
ByVal dict As Object, _
ByVal FirstIndex As Long, _
ByVal SecondIndex As Long)
Dim StartIndex As Long
Dim EndIndex As Long
Dim dStep As Long
' Account for first greater than second.
If FirstIndex <= SecondIndex Then
StartIndex = FirstIndex
EndIndex = SecondIndex
Else
StartIndex = SecondIndex
EndIndex = FirstIndex
End If
' Account for out of bounds.
If StartIndex < 0 Then StartIndex = 0
If EndIndex > dict.Count - 1 Then EndIndex = dict.Count - 1
Dim Key As Variant
Dim n As Long
For Each Key In dict.Keys
Select Case n
Case StartIndex To EndIndex
Debug.Print n, Key, dict(Key) ' if simple datatype
End Select
n = n 1
Next Key
End Sub
早期系結
' Needs a reference to the Microsoft Scripting Runtime library.
Sub PrintDictEarly( _
ByVal dict As Scripting.Dictionary, _
ByVal StartIndex As Long, _
ByVal EndIndex As Long)
' Note that the dictionary indexes are zero-based!
' Note that you can loop backwards!
Dim dStep As Long
If StartIndex <= EndIndex Then ' ascending
dStep = 1
' Account for out of bounds.
If StartIndex < 0 Then StartIndex = 0
If EndIndex > dict.Count - 1 Then EndIndex = dict.Count - 1
Else ' descending
dStep = -1
' Account for out of bounds.
If EndIndex < 0 Then EndIndex = 0
If StartIndex > dict.Count - 1 Then StartIndex = dict.Count - 1
End If
Dim Key As Variant
Dim n As Long
For n = StartIndex To EndIndex Step dStep
Debug.Print n, dict.Keys(n), dict.Items(n) ' if simple datatype
Next n
End Sub
測驗
' Needs a reference to the Microsoft Scripting Runtime library.
Sub LateVsEarly()
Dim dict As Scripting.Dictionary: Set dict = New Scripting.Dictionary
Dim i As Long
For i = 1 To 20
dict.Add i, i ^ 2
Next i
' This works always.
Debug.Print "Late Binding:"
Debug.Print "Index", "Key", "Item"
PrintDictLate dict, 5, 10
' This works only if a reference has been created.
Debug.Print "Early Binding:"
Debug.Print "Index", "Key", "Item"
On Error Resume Next ' prevent error if no reference created
PrintDictEarly dict, 10, 5
On Error GoTo 0
End Sub
結果
Late Binding:
Index Key Item
5 6 36
6 7 49
7 8 64
8 9 81
9 10 100
10 11 121
Early Binding:
Index Key Item
10 11 121
9 10 100
8 9 81
7 8 64
6 7 49
5 6 36
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517059.html
上一篇:System.Text.Json.deserializeJson字串
下一篇:高圖表未在折線圖上正確顯示值
