【发布时间】:2019-02-13 09:37:34
【问题描述】:
我在使用 VBA 中的对象变量时遇到问题。是否可以只复制对象变量而无需任何引用?
这里是类模块“clstest”
Option Explicit
Public x As Single
这是我的潜艇:
Sub CopyWithoutReference()
Dim standard As New clstest
Set standard = New clstest
Dim different As New clstest
standard.x = 20
Set different = standard
different.x = 30
MsgBox "I want standard.x to be 20 and not 30"
MsgBox standard.x
MsgBox different.x
我希望 standard.x 保持其值,并且如果 different.x 发生变化,则不会发生变化。 我在这里读到这篇文章: https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/set-statement 它说:
“因为这些变量是对对象的引用而不是对象的副本,所以对象的任何变化都会反映在引用它的所有变量中。”
但我不知道如何避免这个问题。有谁知道如何帮助我?
【问题讨论】:
标签: vba object reference set copy