首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA组合框/自动生成代码

VBA组合框/自动生成代码
EN

Stack Overflow用户
提问于 2019-01-10 12:22:05
回答 1查看 335关注 0票数 1

我在Excel中有一个关于组合框的问题。

我有一个excel表,默认情况下包含两个组合框,它们的编号由变量x描述(默认情况下是x=2)。每个combobox都是脚本化的,以便在subs中以特定的方式运行,例如:私有子ComboBox1_DropButtonClick()。

尽管如此,有时我需要通过改变X的值来增加这些盒子的数量。我可能总共需要10个组合框。现在的问题是,是否有任何方法可以设置无限多个组合框的行为(例如,在DropButtonClick的情况下)。我所做的就是为每个组合框编写一个代码,所以我为ComboBox1_DropButtonClick()、ComboBox2_DropButtonClick()、ComboBox3_DropButtonClick()等等编写了一个子程序。代码略有变化,但它是可重复的。所以这一切看起来都很愚蠢,我正在寻找一些更巧妙的解决方案。也许所有的组合框都能一次完成脚本?如果有什么办法,请和我分享。

谢谢,沃奇奇。

编辑代码的位置(用灰色标记):VBA编辑器截图

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-10 13:53:55

下面是一些代码,可以动态地将控件添加到Excel用户表单中,并在后面添加代码。添加的代码将使其在MessageBox接收到KeyDown时显示KeyDown

代码有一些注释,但是如果您有问题,请告诉我:)

代码语言:javascript
复制
Option Explicit

Sub CreateFormComboBoxes(NumberOfComboBoxes As Long)
    Dim frm         As Object
    Dim ComboBox    As Object
    Dim Code        As String
    Dim i           As Long

    'Make a blank form called 'UserForm1', or any name you want
    'make sure it has no controls or any code in it
    Set frm = ThisWorkbook.VBProject.VBComponents("UserForm1")

    With frm
        For i = 1 To NumberOfComboBoxes
            Set ComboBox = .designer.Controls.Add("Forms.ComboBox.1")
             'Set the properties of the new controls
             With ComboBox
                .Width = 100
                .Height = 20
                .Top = 20 + ((i - 1) * 40) 'Move the control down
                .Left = 20
                .Visible = True
                .ZOrder (1)
                .Name = "ComboBox" & i
            End With
            'Add your code for each module, you can add different code, by adding a if statement here
            'And write the code depending on the name, index, or something else
            Code = Code & vbNewLine & "Private Sub " & "ComboBox" & i & "_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)" & _
                   vbNewLine & "    MsgBox(""hi"")" & vbNewLine & "End Sub"
        Next

        'Add the code
        .CodeModule.InsertLines 2, Code
    End With

End Sub

'Run this
Sub Example()
    CreateFormComboBoxes 5
End Sub


**Edit**

我想我最好添加另一种方法来动态地将控件添加到Excel表中。我建议坚持使用UserForms,但是,在工作表中需要控件时,这里有一种方法应该可以帮助解决这个问题。

代码语言:javascript
复制
Sub addCombosToExcelSheet(MySheet As Worksheet, NumberOfComboBoxes As Long, StringRangeForDropDown As String)
    Dim i           As Long
    Dim combo       As Shape
    Dim yPosition   As Long
    Dim Module      As Object

    yPosition = 20
    For i = 1 To NumberOfComboBoxes
        yPosition = (i - 1) * 50

        'Create the shape
        Set combo = MySheet.Shapes.AddFormControl(xlDropDown, 20, yPosition, 100, 20)

        ' Range where the values are stored for the dropDown
        combo.ControlFormat.ListFillRange = StringRangeForDropDown
        combo.Name = "Combo" & i
        Code = "Sub Combo" & i & "_Change()" & vbNewLine & _
               "    MsgBox(""hi"")" & vbNewLine & _
               "End Sub"
        'Add the code
        With ThisWorkbook
            'Make sure Module2 Exits and there is no other code present in it
            Set Module = .VBProject.VBComponents("Module2").CodeModule
            Module.AddFromString (Code)
        End With

        'Associate the control with the action, don't include the () at the end!
        combo.OnAction = "'" & ActiveWorkbook.Name & "'!Combo" & i & "_Change"
    Next

End Sub

Sub Example()
    Dim sht As Worksheet: Set sht = ThisWorkbook.Sheets(1)
    addCombosToExcelSheet sht, 10, "Sheet1!$A$1:$A$10"
End Sub
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54128625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档