我如何在新的类对象的声明中使用单元格值?



我有一个相当简单的问题,我似乎找不到答案,所以我将在这里尝试。我创建了一个名为"groups"的新类。vba。我要做的是从excel中的单元格中检索值,并将其用作我要创建的新组的名称。我试过通过Range("B20")访问值。值,但是我需要先将它存储在另一个变量中,如果我这样做,我就不能使用它作为名称,因为它认为我想要像我存储它的变量一样命名组。有人知道怎么解决这个问题吗?谢谢大家的帮助和建议。

下面是我尝试过的,但没有成功。

Dim Range("B20").Value As Group

我也试过这样做,但它认为我想将组命名为变量"getName">

Dim getName As String
getName = Range("B20").Value
Dim getNAme As Group

亲切的问候,Jahlove

正如其他人在评论中建议的那样,您不能将字符串变量更改为变量名。然而,如果你想用字符串访问/引用一个对象,你可以尝试这样的Scripting.Dictionary(文档在这里):

Sub DynamicGroupNaming()
Dim sGroupName As String
Dim dictGroups As Object
Set dictGroups = CreateObject("Scripting.Dictionary")

'Change Sheet1 to the name of your worksheet
sGroupName = ThisWorkbook.Sheets("Sheet1").Range("B20").Value

' Add a new group to the dictionary
dictGroups.Add sGroupName, New Group

' Access the group's properties/methods in the usual way
dictGroups(sGroupName).Name = sGroupName
MsgBox dictGroups(sGroupName).Name
Set dictGroups = Nothing
End Sub

最新更新