更改克隆对象而不更改原始对象

Change cloned object without changing the original object

是否可以在VBA中复制一个对象,使克隆对象的变化不影响原始对象?

例如:

Dim clone_object As Variant
Set clone_object = some_object
some_object.Left = 0
clone_object.Left = 666
'I want it to show 0 instead of 666
Debug.print some_object.Left

您需要在 class 模块(这里是 Config)中创建这样的方法,或者如果它不是自定义对象,则在常规模块中执行相同的操作:

Friend Sub SetConfig(SrcConfig As Config)
    Set Cfg = SrcConfig
End Sub

Public Function Copy() As Config
    Dim Result As Config
    Set Result = New Config
    Call Result.SetConfig(Cfg)
    Set Copy = Result
End Function