我可以用文本框设置变量的名称吗?VBA Excel

我可以用文本框设置变量的名称吗?VBA Excel,excel,vba,Excel,Vba,我可以用文本框(Vba Excel)设置变量的名称吗? 我必须以在文本框中写入组名并单击命令按钮的方式输入新的产品组。 代码必须从文本框中获取字符串,并将该字符串设置为新创建数组的名称。通常不建议这样做。但是,您可以使用编写自己的类,然后保存您的组名和数据。我通常使用集合作为与数组相反的数据容器。类MyClass将如下所示: Option Explicit Private Type TModel Data As Collection GroupName as string En

我可以用文本框(Vba Excel)设置变量的名称吗? 我必须以在文本框中写入组名并单击命令按钮的方式输入新的产品组。
代码必须从文本框中获取字符串,并将该字符串设置为新创建数组的名称。

通常不建议这样做。但是,您可以使用编写自己的类,然后保存您的
组名
数据
。我通常使用
集合
作为与数组相反的数据容器。类
MyClass
将如下所示:

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
可以获取并释放类的属性。在
模块中可以这样使用

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
类型的每个变量都有一个名称和数据

我只想在运行时创建一个新变量(或数组)

人们认为这是不可能的。但事实确实如此。这是没有记录在任何地方(至少我没有阅读它的任何地方)

逻辑

我们只需在运行时编写VBA编辑器,以创建新数组。您可以在Chip的网站上阅读更多有关编程VBA编辑器的信息

注意

Option Explicit

Const vbext_ct_StdModule As Integer = 1

Sub Sample()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Dim CodeMod As VBIDE.CodeModule
    Dim LineNum As Long

    Set VBProj = ThisWorkbook.VBProject
    Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)

    '~~> Create new module called MySpecialModule
    '~~> It it exists then you will get an error
    '~~> Either delete it and create new one or use error handling
    VBComp.Name = "MySpecialModule"

    Set CodeMod = VBComp.CodeModule

    With CodeMod
        LineNum = .CountOfLines + 1
        .InsertLines LineNum, "Public MyArray() As String"
        LineNum = LineNum + 1
        .InsertLines LineNum, "Public Sub InitArray()"
        LineNum = LineNum + 1
        .InsertLines LineNum, "    Redim MyArray (1 to 5)"
        LineNum = LineNum + 1
        .InsertLines LineNum, "End Sub"
    End With

    '~~> Initialize newly created Array
    initializeArray
End Sub

'~~> Run the procedure to initialize the newly created array
Sub initializeArray()
    InitArray

    Debug.Print UBound(MyArray)
End Sub
  • 确保已设置对Microsoft Visual Basic For Applications Extensibility 5.3的引用
  • 我没有做任何错误处理。您可以在代码中随意加入这些内容
  • 在下面的代码中,我们将创建一个数组
    MyArray
    ,并将其尺寸标注为
    5

    代码

    Option Explicit
    
    Const vbext_ct_StdModule As Integer = 1
    
    Sub Sample()
        Dim VBProj As VBIDE.VBProject
        Dim VBComp As VBIDE.VBComponent
        Dim CodeMod As VBIDE.CodeModule
        Dim LineNum As Long
    
        Set VBProj = ThisWorkbook.VBProject
        Set VBComp = VBProj.VBComponents.Add(vbext_ct_StdModule)
    
        '~~> Create new module called MySpecialModule
        '~~> It it exists then you will get an error
        '~~> Either delete it and create new one or use error handling
        VBComp.Name = "MySpecialModule"
    
        Set CodeMod = VBComp.CodeModule
    
        With CodeMod
            LineNum = .CountOfLines + 1
            .InsertLines LineNum, "Public MyArray() As String"
            LineNum = LineNum + 1
            .InsertLines LineNum, "Public Sub InitArray()"
            LineNum = LineNum + 1
            .InsertLines LineNum, "    Redim MyArray (1 to 5)"
            LineNum = LineNum + 1
            .InsertLines LineNum, "End Sub"
        End With
    
        '~~> Initialize newly created Array
        initializeArray
    End Sub
    
    '~~> Run the procedure to initialize the newly created array
    Sub initializeArray()
        InitArray
    
        Debug.Print UBound(MyArray)
    End Sub
    
    运行代码时,将创建一个新模块,并在该模块中注入一些代码。然后在运行时再次调用该代码。运行代码时,以下代码不存在

    Public MyArray() As String
    
    Public Sub InitArray()
        ReDim MyArray(1 To 5)
    End Sub
    

    你试过什么吗?请分享你的努力,没有办法。但是为什么要更改变量的名称?@FunThomas:不,不是这样,您可以在运行时创建变量。不幸的是,这在任何地方都没有被记录(或者至少我没有在任何地方读到它),实际上我不想更改变量的名称。我只想在运行时创建一个新变量(或数组)。我需要检查文本框allready中的字符串是否存在于变量列表中,如果不存在,则在文本框中创建一个名为this string的新变量。
    我只想在运行时创建一个新变量(或数组)。
    您可以这样做,但不建议这样做。推荐使用收藏/字典,如图所示。呵呵,我喜欢这样。然后,在检查变量是否已存在时,在错误恢复下一步中设置
    。当然,最后还要拆下模块。调试是不可能的(抛出一个错误)-没有人能够理解你的代码…这样的问题激励我在我的网站上创建博客。已在上创建博客帖子:D