动态用户窗体设置

时间:2017-08-31 17:29:20

标签: excel-vba vba excel

我正在尝试设计一个可以使用设置的用户表单,如下图所示。

Excel Spreadsheet View

结果,我开发了这个设置。

UserForm View

由于会有多个区域(在不同列上有多个列表),我首先尝试解决动态切换按钮的选择问题。我开发了一种为每个切换按钮和每个标签添加代码的方法,但我不确定如何使用框架或滚动条。这是我到目前为止完成的编码,我在如何使用框架方面遇到了各种各样的困难。

Private Sub UserForm_Click()
Unload Me
BreakAtWill.Show
End Sub

'Pulling in relevant information
'Selecting the area
Private Sub UserForm_Initialize()
Void = InsertTBs
End Sub



Function InsertTBs()

'Variable Declaration
'UserForm Declarations
Dim myForm As Object
Dim NewPart As MSForms.Label
Dim NewMachine As MSForms.Label
Dim NewToggleButton As MSForms.ToggleButton

'Counter Variables
Dim Parts As Integer
Dim Machines As Integer
Dim TBN As Integer

'Holder Variables
Dim TotalParts As Integer
Dim TotalMachines As Integer



'Initializing Variables
'UserForm
Set myForm = ThisWorkbook.VBProject.Item("BreakAtWill")
''Frames
'Set PartFrame = Frame1_PartType.Controls.Add("Forms.Frame.1", "lbl1")
'Set MachineFrame = Frame2_Machine.Controls.Add("Forms.Frame.1", "lbl1")
'Set TBFrame = Frame3_U_NU.Controls.Add("Forms.Frame.1", "lbl1")


'Variables
TBN = 0
TotalParts = 5
TotalMachines = 5


'Calculations
'Part Types
For Parts = 1 To TotalParts

    'New Part Type
    Set NewPart = myForm.designer.Controls.Add("Forms.label.1")
    'Set NewPart = myForm.Frame1_PartType.Controls.Add("Forms.label.1", "PartType" & Parts)

    'New Part's Properties
    With NewPart
        'Appearance
        .Name = "PartType" & Parts
        'Position
        .Top = 12
        .Left = 12 + 36 * (Parts) ' - 1)
        .Width = 29.25
        .Height = 29.25
        'Misc
        .Caption = "Parts" & Parts
    End With
 Next Parts



'Machines
'Machines could be nested inside the Toggle Buttons loop, but that makes this harder to read.
'Plus during development, moving this inside the loop did not affect the time for the userform to appear (Bias: done on small scale).
For Machines = 1 To TotalMachines

    'New Machines
    Set NewMachine = myForm.designer.Controls.Add("Forms.label.1")
    'Set NewMachine = MachineFrame.designer.Controls.Add("Forms.label.1")

    'Machines's Properties
    With NewMachine
        'Appearance
        .Name = "Machine" & Machines
        'Position
        .Top = 12 + 36 * (Machines) ' - 1)
        .Left = 12
        .Width = 29.25
        .Height = 29.25
        'Misc
        .Caption = "Machine" & Machines
    End With
 Next Machines



'Toggle Buttons
For Machines = 1 To TotalMachines
    For Parts = 1 To TotalParts

        'New Toggle Button Number
        TBN = TBN + 1

        'New Toggle Button
        Set NewToggleButton = myForm.designer.Controls.Add("Forms.togglebutton.1")
        'Set NewToggleButton = TBFrame.designer.Controls.Add("Forms.togglebutton.1")

        'Toggle Button's Properties
        With NewToggleButton
            'Appearance
            .Name = "ToggleButton" & TBN
            'Position
            .Top = 12 + 36 * (Machines) ' - 1)
            .Left = 12 + 36 * (Parts) ' - 1)
            .Width = 29.25
            .Height = 29.25
            'Misc
            .TabIndex = TBN
            .Caption = "Not Used"
        End With

        'Toggle Button's Code
        'Places the code at the top of this UserForm's code
        myForm.codemodule.insertlines 1, "'Toggle Button " & TBN
        myForm.codemodule.insertlines 2, "Private Sub ToggleButton" & TBN & "_AfterUpdate()"
        myForm.codemodule.insertlines 3, "If ToggleButton" & TBN & ".Value = True Then ToggleButton" & TBN & ".Caption = ""Used""       'Used State "
        myForm.codemodule.insertlines 4, "If ToggleButton" & TBN & ".Value = False Then ToggleButton" & TBN & ".Caption = ""Not Used""  'Not Used State "
        myForm.codemodule.insertlines 5, "End Sub"

        'Updating Counters
    Next Parts
Next Machines


End Function

我对vba所提供的内容以及你们需要提供的信息仍然不熟悉,所以如果我需要提供更多信息,我将很乐意回复。

0 个答案:

没有答案