在VBA中设置对象的麻烦

时间:2016-12-26 17:11:08

标签: vba object hyperlink

也许这个问题很简单,但我无法通过Google搜索找到答案。

所以,我已经获得了我的课程WSheet。我在程序中初始化了这个类的对象数组:

Dim SName As String 'Name of sheet
Dim SWeight As Long 'Weight of sheet in bytes
Dim blocks() As Long 'Weights of blocks in sheet in bytes

Public Function Weight() As Long
    Weight = SWeight
End Function

Public Sub SetWeight(ByVal sw As Long)
    SWeight = sw
End Sub

Public Function Name() As String
    Name = SName
End Function

Public Sub SetName(ByVal nm As String)
    SName = nm
End Sub

然后,我尝试读取新值并按插入排序对它们进行排序:

{{1}}

所以,问题是:在我设置WSheets(j)= WSheets(j - 1)之后,wsheets(j - 1)开始包含到wsheets(j)的链接,因此,当我更改wsheets(j)时,Wsheets (j - 1)也改变了。

请分享如何在此样本中制作绝对等式?

谢谢!

WSheet类中的PS代码

{{1}}

1 个答案:

答案 0 :(得分:0)

已修改以将该功能转换为Class方法

您必须克隆 WSheet对象而不是引用它

例如,您可以在Clone()

中添加WSheet方法
Function Clone() As WSheet
    Dim newWSheet As WSheet

    Set newWSheet = New WSheet
    newWSheet.SetName SName
    newWSheet.SetWeight SWeight
    Set Clone = newWSheet
End Function

然后在主代码中更改:

Set WSheets(j) = WSheets(j - 1)

要:

Set WSheets(j) = WSheets(j - 1).Clone