Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA命令按钮数组_Vba_Class_Dynamic_Control Array - Fatal编程技术网

VBA命令按钮数组

VBA命令按钮数组,vba,class,dynamic,control-array,Vba,Class,Dynamic,Control Array,我目前正在进行一个项目,在这个项目中,我将选择最多5个项目进行相互比较,结果将显示在最多5x5个动态网格中。我的目标是让这个网格由命令按钮组成,这样每个按钮的标题都是行和列项目之间的相似度百分比,单击按钮后,行和列项目之间的公共单位将显示在消息框中 我或多或少知道如何生成实际的按钮数组。然而,我读过的所有内容都表明,我需要创建一个类来处理按钮点击,因为我不想创建20个子程序,它们都有相同的代码。我无法让这门课正常运行,我需要一些提示。这是我到目前为止所拥有的 在名为DynButton的类模块中:

我目前正在进行一个项目,在这个项目中,我将选择最多5个项目进行相互比较,结果将显示在最多5x5个动态网格中。我的目标是让这个网格由命令按钮组成,这样每个按钮的标题都是行和列项目之间的相似度百分比,单击按钮后,行和列项目之间的公共单位将显示在消息框中

我或多或少知道如何生成实际的按钮数组。然而,我读过的所有内容都表明,我需要创建一个类来处理按钮点击,因为我不想创建20个子程序,它们都有相同的代码。我无法让这门课正常运行,我需要一些提示。这是我到目前为止所拥有的

在名为DynButton的类模块中:

Public Withevents CBevents as MSForms.CommandButton
Private Sub CBevents_Click()
    DisplayOverlappedUnits 'Sub that will display the units that are the same
                           'between items i and j- may use Application.Caller
End Sub
在用户表单本身中:

      Private Sub Userform_Initialize()
        Dim NumItems as integer
        Dim ComparisonArray() as DynButton
        Dim ctlButton as MSForms.CommandButton
        'QuestionList() is a public type that stores various attributes of the 
        'items I'm comparing.

       'This code determines how many items were selected for comparison
       'and resets the item array accordingly.
       NumItems=0
       For i=1 to 5 
           If QuestionList(i).Length>0 Then
              NumItems=Numitems+1
              QuestionList(NumItems)=QuestionList(i)
           End If
       Next

Redim ComparisonArray(1 to NumItems, 1 to NumItems)
For i = 1 to NumItems
    For j=1 to NumItems
        Set ctlButton=Me.Controls.Add("Forms.CommandButton.1", Cstr(i) & Cstr(j) & cb)
        With ctlButton
            .Height= CB_HEIGHT 'These are public constants defined elsewhere.
            .Width= CB_WIDTH
            .Top= TOP_OFFSET + (i * (CB_HEIGHT+ V_PADDING))
            If i = j Then .visible = False
            .Caption= CalculateOverlap(i,j) 'Runs a sub that calculates the overlap between items i and j
            End With
        Set ComparisonArray(i,j).CBevents = ctlButton
        Next
    Next
End Sub
当前,当我点击set ComparisonArray行时,我得到一个“未设置对象或块变量”,我被阻止了。我是不是在课堂模块中遗漏了什么?提前谢谢你的帮助


编辑添加:在本文的部分内容中,我尝试对类代码进行建模,但仍然没有成功

您的代码似乎正确且有趣。我能看到的唯一(bug)是:

Redim ComparisonArray(1 to NumItems, 1 to NumItems)
...
Set ComparisonArray(i,j).CBevents = ctlButton
问题是数组包含空引用。您尚未创建
DynButton
对象。必须在数组中显式创建对象

Redim ComparisonArray(1 to NumItems, 1 to NumItems)
For i = 1 to NumItems
    For j = 1 to NumItems
       Set ComparisonArray(i,j) = new DynButton
    Next
Next        
...
Set ComparisonArray(i,j).CBevents = ctlButton
另外,将数组ComparisonArray声明为表单的成员对象,而不是表单_Initialize中的局部变量。

私有子用户表单_Initialize()
Private Sub Userform_Initialize()
        Dim NumItems as integer
        Dim ComparisonArray() as DynButton  '<<<< should be a Global variable
作为整数的Dim NumItems
Dim ComparisonArray()作为动态按钮“仅复制粘贴

Option Private Module
Option Explicit

Private Const i_total_channels As Integer = 100

Sub createArrayOfbuttons()
    Application.ScreenUpdating = False
    f_create_buttons 5, 5, 30, 5, True
End Sub

Sub clearArrayOfButtos()
    Application.ScreenUpdating = False
    f_clear_array_of_buttons
End Sub

Private Function f_create_buttons(Optional posLeft As Integer = 0, Optional posTop As Integer = 0, _
    Optional sizeSquare As Integer = 20, Optional distBetween As Integer, Optional buttonColor As Boolean = False)
'create customized buttons to channel choice.
    Dim i_ch_amount_x As Integer
    Dim i_ch_amount_y As Integer
    Dim i_size_X 'size of square button
    Dim i_size_Y 'size of square button
    Dim i_stp_X As Integer 'step in X
    Dim i_stp_Y As Integer 'step in Y
    Dim i_dist_bte_buttons As Integer 'distance between buttons, in X and Y
    Dim i_pos_ini_X As Integer 'initial position
    Dim i_pos_ini_Y As Integer
    Dim it_x As Integer 'iterator
    Dim it_y As Integer 'iterator
    Dim amount As Integer 'channel acumulator
    Dim FO_color As Integer 'index from 1 to 12 to change background color of button

    f_clear_array_of_buttons

    i_pos_ini_X = posLeft
    i_pos_ini_Y = posTop

    'create dimensions of square
    i_size_X = sizeSquare
    i_size_Y = i_size_X 'to create a square Y need same size of X

    'distance between squares
    i_dist_bte_buttons = i_size_X + distBetween 'to shift distance change laste value of expression
    i_stp_X = i_pos_ini_X

    i_stp_Y = i_pos_ini_Y


    i_ch_amount_x = Int(Sqr(i_total_channels)) 'total channels in switch (i_ch_amount_y * i_ch_amount_x)
    i_ch_amount_y = i_ch_amount_x

    amount = 1
    FO_color = 1
    For it_y = 1 To i_ch_amount_x
        For it_x = 1 To i_ch_amount_y
            f_create_button amount, i_stp_X, i_stp_Y, CSng(i_size_X), CSng(i_size_Y), FO_color
            i_stp_X = i_stp_X + i_dist_bte_buttons
            amount = amount + 1
            If buttonColor Then
                FO_color = FO_color + 1
            End If
            If FO_color > 12 Then 'return FO to 1
                FO_color = 1
            End If
        Next it_x
        i_stp_X = i_pos_ini_X
        i_stp_Y = i_stp_Y + i_dist_bte_buttons
    Next it_y

    amount = 0
    i_ch_amount_x = 0
    i_ch_amount_y = 0
    i_size_X = 0
    i_size_Y = 0
    i_stp_X = 0
    i_stp_Y = 0
    i_pos_ini_X = 0
    i_pos_ini_Y = 0
    i_dist_bte_buttons = 0
    FO_color = 0
End Function

Private Function f_create_button(index As Integer, posLeft As Integer, posRight As Integer, _
    Box_width As Single, Box_height As Single, Optional FO As Integer)
    ActiveSheet.Shapes.AddShape(msoShapeRectangle, posLeft, posRight, Box_width, Box_height). _
        Select
    With Selection
        .Name = "ch_" & index
        .Text = index
        .Font.Name = "Arial"
        .Font.Bold = True
        If FO = 9 Then
            .Font.Color = vbWhite
        Else
            .Font.ColorIndex = xlAutomatic
        End If
        .Font.Size = 10
        .Interior.Color = fiber_color(FO)
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
End Function

Public Function fiber_color(Optional FO As Integer = 1) As Long
'use with a index in FO from 1 to 12
    Select Case FO
    Case 1
        fiber_color = 65280 'green
    Case 2
        fiber_color = 65535 'yellow
    Case 3
        fiber_color = 16777215 'white
    Case 4
        fiber_color = 16711680 'blue
    Case 5
        fiber_color = 255 'red
    Case 6
        fiber_color = 16711823 'violt
    Case 7
        fiber_color = 19350 'brown
    Case 8
        fiber_color = 13353215 'pink
    Case 9
        fiber_color = 0 'black
    Case 10
        fiber_color = 16711680 'cinza
    Case 11
        fiber_color = 32767 'orange
    Case 12
        fiber_color = 16776960 'aqua
    Case Else
        fiber_color = 65280 'verde
    End Select
End Function

Private Function f_clear_array_of_buttons()
    Dim i_ch_amount_x As Integer
    Dim it As Integer

    i_ch_amount_x = i_total_channels
    On Error GoTo sair
    If ActiveSheet.Shapes.Count <> 0 Then
        For it = 1 To i_ch_amount_x
            ActiveSheet.Shapes("ch_" & it).Delete
        Next it
    End If
sair:
    i_ch_amount_x = 0
    it = 0
End Function
选项专用模块
选项显式
专用常量i_总通道数为整数=100
子createArrayOfbuttons()
Application.ScreenUpdating=False
f_创建按钮5、5、30、5、True
端接头
子clearArrayOfButtos()
Application.ScreenUpdating=False
f\u清除\u按钮数组\u
端接头
私有函数f_创建按钮(可选posLeft为整数=0,可选posTop为整数=0_
可选大小平方为整数=20,可选距离间隔为整数,可选按钮颜色为布尔值=False)
'为频道选择创建自定义按钮。
Dim i_ch_金额作为整数
Dim i__________________y作为整数
尺寸i_尺寸X’方形按钮的尺寸
方形按钮的尺寸
尺寸i_stp_X作为整数“步进X”
尺寸i_stp_Y作为Y中的整数“步长”
尺寸i_dist_bte_按钮作为按钮之间的整数距离,以X和Y为单位
尺寸i_位置i_ini_X作为整数的初始位置
Dim i_pos_ini_Y作为整数
将其调暗为整数迭代器
将其设置为整数迭代器
作为“整数”通道累积器的调暗量
将fou color作为整数的索引从1变为12,以更改按钮的背景色
f\u清除\u按钮数组\u
i_pos_ini_X=posLeft
i_pos_ini_Y=后期
'创建正方形的尺寸
i_size_X=尺寸方形
i_size_Y=i_size_X'要创建正方形Y,需要相同大小的X
“正方形之间的距离
i_dist_bte_按钮=i_size_X+distBetween'移动距离更改表达式的最后值
i_stp_X=i_pos_ini_X
i_stp_Y=i_pos_ini_Y
i____金额_x=Int(Sqr(i_总计_通道))'交换机中的总计通道(i_总计_金额_y*i_总计_x)
i____金额_y=i___金额_x
金额=1
FO_color=1
对于它,y=1到i\u ch\u数量
对于它,x=1到i\u ch\u数量
f_创建_按钮数量、i_stp_X、i_stp_Y、CSng(i_size_X)、CSng(i_size_Y)、FO颜色
i_stp_X=i_stp_X+i_dist_bte_按钮
金额=金额+1
如果按钮是彩色的,那么
FO_color=FO_color+1
如果结束
如果FO_color>12,则“将FO返回到1”
FO_color=1
如果结束
接下来是
i_stp_X=i_pos_ini_X
i_stp_Y=i_stp_Y+i_dist_bte_按钮
下一步是
金额=0
i_Chu_金额_x=0
i_ch_金额_y=0
i_size_X=0
i_size_Y=0
i_stp_X=0
i_stp_Y=0
i_pos_ini_X=0
i_pos_ini_Y=0
i_dist_bte_按钮=0
FO_color=0
端函数
私有函数f_create_按钮(索引为整数,posLeft为整数,posRight为整数_
方框\宽度为单个,方框\高度为单个,可选FO为整数)
ActiveSheet.Shapes.AddShape(msoShapeRectangle、posLeft、posRight、Box\u宽度、Box\u高度)_
挑选
有选择
.Name=“ch_389;”和索引
.Text=索引
.Font.Name=“Arial”
.Font.Bold=True
如果FO=9,则
.Font.Color=vbWhite
其他的
.Font.ColorIndex=xlAutomatic
如果结束
.Font.Size=10
.Interior.Color=纤维颜色(FO)
.HorizontalAlignment=xlCenter
.垂直对齐=xlCenter
以
端函数
公共功能光纤颜色(可选FO为整数=1)长度为
'与FO中从1到12的索引一起使用
选择案例FO
案例1
光纤颜色=65280'绿色
案例2
纤维颜色=65535'黄色
案例3
纤维颜色=16777215'白色
案例4
光纤颜色=16711680'蓝色
案例5
纤维颜色=255'红色
案例6
纤维颜色=16711823'维奥特
案例7
纤维颜色=19350'棕色
案例8
纤维颜色=13353215'粉红色
案例9
纤维颜色=0'黑色
案例10
纤维颜色=16711680'辛扎
案例11
纤维颜色=32767'橙色
案例12
纤维颜色=16776960'浅绿色
其他情况
纤维颜色=65280'绿色
结束选择
端函数
专用函数f_clear_array_of_按钮()
Dim i_ch_金额作为整数
将其设置为整数
i_ch_数量x=i_总通道数
关于GoTo-sair错误
如果ActiveSheet.Shapes.Count为0,则
对于it=1到i_ch____数量
ActiveSheet.Shapes(“ch_”&it).De