是否可以在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
答案 0 :(得分:0)
您需要在类模块中创建这样的方法(此处为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