VBA变量作为CommandButton# [英] VBA Variable as CommandButton#

查看:44
本文介绍了VBA变量作为CommandButton#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新编写一些代码并且有一个想法,但是似乎无法正确使用我的语法来正确执行它.我想使用一个for循环来填充命令按钮数组以及控制它们的可见性.我仅需要语法帮助即可定义循环中正在使用的CommandButton编号.例如,CommandButton1,CommandButton2等.

I'm rewriting some code and had a thought, but can't seem to get my syntax right to execute it properly. I want to use a for loop to populate an array of commandbuttons as well as control their visibility. I just need help with my syntax to define which CommandButton number I'm working on in the loop. For instance, CommandButton1, CommandButton2, etc.

Public Sub LoadLots(sName As String, streamLots() As String)
    Label1.Caption = sName
    For o = 1 To 9
        If streamLots(o) <> "" Then
            CommandButton& o &.Caption = streamLots(o)
            CommandButton& o & .Visable = True
        Else
            CommandButton& o & .Visable = False
        End If
    Next
End Sub

推荐答案

使用Userform.Controls集合按名称引用命令按钮.

Use the Userform.Controls collection to reference the commandbuttons by name.

Public Sub LoadLots(sName As String, streamLots() As String)
    Dim btn As MSForms.CommandButton
    Label1.Caption = sName
    For o = 1 To 9
        Set btn = Me.Controls("CommandButton" & o)
        If streamLots(o) <> "" Then
            btn.Caption = streamLots(o)
            btn.Visible = True
        Else
           btn.Visible = False
        End If
    Next
End Sub

这篇关于VBA变量作为CommandButton#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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