我可以在文本框中设置变量的名称吗?VBA Excel [英] Can I set the name of variable with textbox? VBA Excel

查看:61
本文介绍了我可以在文本框中设置变量的名称吗?VBA Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用TextBox(Vba Excel)设置变量的名称吗?我必须以在文本框中输入组名称的方式输入新的产品组,然后单击命令按钮.代码必须将文本框中的字符串"设置为新创建的数组的名称.

Can I set the name of the variable with TextBox (Vba Excel)? I have to input new group of products on the way that I write the name of the group in TextBox and click command button. The code have to take the String from Textbox an set this string as name of new created array.

推荐答案

通常不建议这样做.但是,您可以使用自己的类进行编程,然后保存您的 GroupName Data .我通常使用 collections 作为与数组相对的数据容器.然后,类 MyClass 如下所示:

In general it is not advisable to do that. You could however program your own classe with would then hold your GroupName and Data. I usually use collections as data containers opposed to arrays. A class MyClass would then look like this:

Option Explicit

Private Type TModel
    Data As Collection
    GroupName as string
End Type

Private this As TModel

Public Property Get GroupName() As Collection
    Set GroupName= this.GroupName
End Property
Public Property Let GroupName(ByVal Value As Collection)
    Set this.GroupName= Value
End Property
Public Property Get Data() As Collection
    Set Data = this.Data
End Property
Public Property Let Data(ByVal Value As Collection)
    Set this.Data = Value
End Property

Private Sub Class_Initialize()
Set this.Data = New Collection
End Sub

这看起来很复杂,但实际上非常简单.您使用私有类型 TModel 进行封装.您在此处存储数据.然后,使用 Property Get Property Let ,您可以获取并出租您的类的属性.在 Module 中可以这样使用.

This looks pretty complicated but actually is very simple. You have a encapsulation using the Private Type TModel. There you store the data. Using Property Get and Property Let you can then get and let your property of the class. This may be used like this in a Module.

Sub TestMyClass()
Dim MClass as New MyClass
Dim GName as String
Dim Data as New Collection

Set Data=GetData()
GName=GetName()

Set MClass.Data=Data
Set MClass.GroupName=GName

Debug.Print MClass.GroupName ' Prints Groupname
End Sub

例如,每个 MyClass 类型的变量都具有名称和数据.

Like this every Variable of the type MyClass has a name and data in it.

这篇关于我可以在文本框中设置变量的名称吗?VBA Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆