2016-12-16 56 views
0

我正嘗試使用vba字典創建數據清理映射。我存儲一系列國家代碼的值,如FR,BE,NL作爲鍵以及它們的偏移值作爲項目:法國,比利時,荷蘭......當我運行測試並嘗試使用字符串作爲鍵檢索值時,拋出運行時錯誤451'沒有返回對象'誰能告訴我可能是什麼問題?無法使用字符串鍵調用VBA詞典項

Sub getthisdone() 

'Dim dict As scripting.dictionary 
Dim ws As Worksheet 
Dim lastRow As Long 
Dim key As String, dictItem As String 
Dim i As Long 

Set ws = ThisWorkbook.Worksheets("Country mapping") 
Set dict = CreateObject("Scripting.Dictionary") 

lastRow = ws.Range("A1").SpecialCells(xlCellTypeLastCell).Row 

For i = 2 To 8 'lastRow 

key = ws.Cells(i, 1).Text 
dictItem = ws.Cells(i, 2).Text 

    With dict 

     .Add key, dictItem 

    End With 

Next i 

MsgBox dict.items("FR") '<---- Error happens here, why? 



End Sub 

回答

2

.Item.Items

MsgBox dict.Item("FR") 

如果單擊項目參考,勾選 「Microsoft腳本運行」,改變CreateObject行:

Dim dict As Scripting.Dictionary: Set dict = New Scripting.Dictionary 

你會獲得早期綁定自動完成並避免這樣的拼寫錯誤。

相關問題