【问题标题】:Can I set the name of variable with textbox? VBA Excel我可以用文本框设置变量的名称吗? VBA Excel
【发布时间】:2019-03-22 18:34:30
【问题描述】:

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

【问题讨论】:

  • 你有没有尝试过?请分享你的努力
  • 没办法。但是为什么要更改变量的名称呢?
  • @FunThomas:不,这不是真的您可以在运行时创建变量。不幸的是,这在任何地方都没有记录(或者至少我没有在任何地方读过它)
  • 其实我并不想改变量名。我只想在运行时创建一个新变量(或数组)。我需要检查 TextBox 中的字符串是否已经存在于变量列表中,如果不存在,则在 TextBox 中创建一个名为此字符串的新变量。
  • I just want to create a new variable (or array) at runtime. 你可以这样做,但不建议这样做。建议使用集合/字典,如图所示Here

标签: excel vba


【解决方案1】:

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

据信这是不可能的。但它是。这在任何地方都没有记录(至少我没有在任何地方读过它)。

逻辑

我们将简单地在运行时对 VBA 编辑器进行编程以创建新数组。您可以在 Chip 的网站Programming The VBA Editor 上阅读有关 VBA 编辑器编程的更多信息

注意

  1. 确保您已设置对Microsoft Visual Basic For Applications Extensibility 5.3 的引用
  2. 我没有进行任何错误处理。随意将其合并到您的代码中。

在下面的代码中,我们将创建一个数组 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

【讨论】:

  • 呵呵,我喜欢。然后在检查变量是否已存在时输入On error resume next。当然,最后删除模块。无法进行调试(抛出错误)- 没有人能够理解您的代码...
  • 这样的问题激励我在自己的网站上创建博客。在Create a new variable/object at runtime 上创建了一篇博文:D
【解决方案2】:

一般不建议这样做。但是,您可以编写自己的课程,然后使用您的GroupNameData。我通常使用collections 作为与数组相对的数据容器。一个类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 GetProperty Let,您可以获取并让您的班级财产。这可以在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 类型的每个变量都有一个名称和数据。

【讨论】:

    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多