VBA: Dynamical Userform from Module

lilcrisis

New Member
Joined
Jul 6, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello! :)

I'm quite new to VBA and trying to write a Module Sub that creates a dynamical UserForm and calls it.
In detail, I'm trying to do the following:

In my Sub "Note" there is an array called “wahlpflicht” that contains names which should be displayed in my UserForm (I tried to put them in as labels).
Each name should have two checkboxes and a TextBox at its side.
After filling out the checkboxes and the textbox the user's input should be confirmed by clicking a button (button1).
After the confirmation, I want to use the given input per each Wahlpflicht-name (checkbox 1 or 2 checked? content of the textbox?) forward in my Sub Note.

I already managed to create the UserForm with the desired names and checkboxes/textboxes. But now I don't know how to force my Sub "Note" to continue only after clicking the button1.
Could anyone please help? :)


My code so far is the following:

Module1:
VBA Code:
Sub Note()

Dim wahlpflicht() As String, size2 As Integer
...
Call UserForm1.addLabel(CInt(n), wahlpflicht)

UserForm1:
VBA Code:
Sub addLabel(n As Integer, ByRef names() As String)
UserForm1.Show vbModeless
Dim theLabel As Object
Dim theInput As Control
Dim edtBox_n As Control
Dim labelCounter As Long

With Me
    For labelCounter = 0 To n
    
        Set theLabel = UserForm1.Controls.Add("Forms.Label.1", "Label" & labelCounter, True)
        With theLabel
            .Caption = names(labelCounter)
            .Width = 500
            .Height = 30
            .Left = 10
            .Top = 50 * (labelCounter + 1)
        End With
        
        Set Cbx = UserForm1.Controls.Add("Forms.CheckBox.1", "Checkbox" & labelCounter, True)
        With Cbx
            .Top = 50 * (labelCounter + 1)
            .Left = 200
            .Caption = "Check"
        End With
        
        Set Cbx2 = UserForm1.Controls.Add("Forms.CheckBox.1", " ", True)
        With Cbx2
            .Name = "CB_" & labelCounter
            .Top = 50 * (labelCounter + 1)
            .Left = 300
            .Caption = "Check"
        End With
        
        Set theInput = UserForm1.Controls.Add("Forms.TextBox.1", "theInput", True)
        With theInput
            .Top = 50 * (labelCounter + 1)
            .Left = 400
            '.MultiLine = True
            .EnterKeyBehavior = True
            .Height = 30
            .Width = 80
        End With
    Next
End With

With UserForm1
    If .Tag = "OK" Then
        For Each myControl In UserForm1.Controls
            If (TypeName(myControl) = "CheckBox") Then
                If (myControl.Value = True) Then
                    MsgBox (myControl.Name)
                End If
            End If
        Next
    End If
End With
'Unload UserForm1

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,223,929
Messages
6,175,461
Members
452,645
Latest member
Tante

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top