用计数器选择变量对象 [英] Select variable object with counter

查看:25
本文介绍了用计数器选择变量对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我在用户表单中有一个使用标准化名称的对象集合(例如 Listbox 对象),我想使用计数器循环动态重命名它们.

Background:
I have a collection of objects (for this example Listbox objects) in a userform using standardized names, I would like to rename them dynamically using a counter cycle.


问题:
我还没有想出一个方法,如果我问的是什至可能的话,但是,我想确认一下.解决方法:
到目前为止没有任何事情,就像我说的(参考上图)我需要一种方法来设置 for 循环中对象的值,如下所示:


Problem:
I have not figured a way if what I am asking is even possible, however, I would like to confirm it. Solution approach:
Nothing so far, like I said (refer to the image above) I need a way to set the values of the objects within the for cycle, something like this:

For CounterItems = 1 To 18 'Hours in Template
ListBox_Time(CounterItems).Value="Dummy" & CounterItems
Next CounterHours

但是,我对如何做到这一点(或者是否可以实现)一无所知.
问题:
有没有办法使用计数器来转换变量/对象?

However, I am clueless on how to do so (or if it is achievable).
Question:
Is there any way to use a counter to cast a variable/object?

推荐答案

用于用户窗体上的 ListBox 控件

如果您只想按编号更改某些 ListBox 控件:

Public Sub ListBoxNameChange()
    Dim ctrl As Control
    Dim ctrlName As String, ctrlNum As Integer

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "ListBox" Then
            ctrlName = ctrl.Name
            ctrlNum = CInt(Replace(ctrlName, "ListBox_Time", "")) 
            If ctrlNum > 0 And ctrlNum < 19 Then
                ctrl.AddItem "Dummy" & ctrlNum, 0
            End If
        End If
    Next ctrl
End Sub

如果要更改所有 ListBox 控件:

Public Sub ListBoxNameChange2()
    Dim ctrl As Control
    Dim ctrlName As String

    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "ListBox" Then _
            ctrl.AddItem "Dummy" & Replace(ctrl.Name, "ListBox_Time", ""), 0
    Next ctrl
End Sub

这篇关于用计数器选择变量对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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