【问题标题】:Dynamically extract text from a userform从用户表单中动态提取文本
【发布时间】:2018-08-06 02:29:07
【问题描述】:

我想从用户窗体中的文本框中提取文本并将它们作为元素存储在数组中。但是,这些文本框具有动态名称,我无法设置数组元素,因为它将它们存储为字符串。

'Loop through the sections to fill the array using a concatonated dynamic path
For Counter = 0 To numSections - 1

    'Fill in title, start page, end page
    sectionInfo(Counter, 0) = "UserForm1.TextInput" & CStr(Counter) & ".Text"

    sectionInfo(Counter, 1) = "UserForm1.pageStart" & CStr(Counter) & ".Text"

    sectionInfo(Counter, 2) = "UserForm1.pageEnd" & CStr(Counter) & ".Text"

Next Counter

如何连接字符串并将其作为命令而不是字符串传递?我开始认为这是不可能的,因为我到处都看过。

【问题讨论】:

  • 使用Controls 集合。 UserForm1.Controls("TextInput" & CStr(Counter)).Text 之类的东西。
  • 这就像一个魅力。非常感谢文森特!
  • @VincentG,如果你把你的评论变成答案,我们可以投票。

标签: arrays vba variables dynamic userform


【解决方案1】:

使用Controls collectionUserForm1.Controls("TextInput" & CStr(Counter)).Text 之类的东西。

'Loop through the sections to fill the array using a concatonated dynamic path
For Counter = 0 To numSections - 1
    'Fill in title, start page, end page
    sectionInfo(Counter, 0) = UserForm1.Controls("TextInput" & CStr(Counter)).Text
    sectionInfo(Counter, 1) = UserForm1.Controls("pageStart" & CStr(Counter)).Text
    sectionInfo(Counter, 2) = UserForm1.Controls("pageEnd" & CStr(Counter)).Text
Next Counter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多