共用方式為


Remove 方法

字典 物件中移除一對鍵/項目。

語法

物件移除 (鑰匙)

Remove 方法語法包含以下部分:

部分 描述
物件 此為必要動作。 總是字 物件的名稱。
key 此為必要動作。 鍵與你想從字典物件中移除的鍵/項目對相關聯

註解

若指定的鍵/項目對不存在,則會發生錯誤。

以下程式碼說明了 Remove 方法的使用。

Public Sub Start()

    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")
    
    d.Add "a", "Athens"
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"
    
    Debug.Print "Keys, before using Remove."
    PrintKeys d
    
    d.Remove "b"
    
    Debug.Print "Keys, after removing key 'b'."
    PrintKeys d

End Sub

Private Sub PrintKeys(ByVal d As Object)
    
    Dim k As Variant
    For Each k In d.Keys
        Debug.Print k
    Next k
    
End Sub

' The example displays the following output:
' Keys, before using Remove.
' a
' b
' c
' Keys, after removing key 'b'.
' a
' c

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應