【问题标题】:Change cloned object without changing the original object更改克隆对象而不更改原始对象
【发布时间】:2016-02-21 08:11:11
【问题描述】:

是否可以在 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

【问题讨论】:

  • 那个例子根本没有帮助——这取决于你正在使用什么对象,但前提是你需要创建一个对象的新实例,这段代码只是分配原始的对象到另一个变量 - 不是“克隆”它
  • 微距人,这是一个图像

标签: vba object set clone cloning


【解决方案1】:

你需要在你的类模块(这里是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

【讨论】:

  • R3uK,最终解决了我的问题。非常感谢
  • 很高兴我能帮上忙!享受吧! ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2020-03-03
相关资源
最近更新 更多