爲了做到這一點,您需要將值保存到註冊表中。雖然您可以調用Win API來執行此操作,但如果您在Mac上運行您的代碼,它會有點複雜並且無法正常工作。
而是使用內置的VBA SaveSetting和GetSetting命令。
Sub Thing()
' Save some values to the registry:
SaveSetting "MyApp", "Settings", "MySetting1", "MyValue1"
SaveSetting "MyApp", "Settings", "MySetting2", "MyValue2"
SaveSetting "MyApp", "Settings", "MySetting3", "MyValue3"
' Saves Setting/Value pairs to
' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyApp\Settings
' Get one of the saved values:
Debug.Print GetSetting("MyApp", "Settings", "MySetting1")
' Get one of the saved values and return a default value if there's no such saved value:
Debug.Print GetSetting("MyApp", "Settings", "MySetting42", "The Answer To Life, Universe, et al")
' List
Dim lCount As Long
Dim MySettings As Variant
MySettings = GetAllSettings("MyApp", "Settings")
For lCount = LBound(MySettings, 1) To UBound(MySettings, 1)
Debug.Print MySettings(lCount, 0), MySettings(lCount, 1)
Next
' Remove all of the settings:
DeleteSetting "MyApp", "Settings"
End Sub