Quantcast
Channel: VBA Code pur...
Viewing all articles
Browse latest Browse all 93

UserForm, ComboBox, TextBox - variabel ansprechen...

$
0
0
Frage: Userformen, ComboBoxen und Textboxen variabel ansprechen. Ich habe mehrere UserFormen. Auf Grund von Usereingaben muss eine bestimmte UserForm geöffnet werden. Eine TextBox soll schon einen bestimmten Text enthalten. Eine ComboBox einen bestimmten Wert anzeigen. Wie geht das?

User forms, combo boxes and text boxes respond variably. I have several user forms. Based on user input, a certain UserForm to open. A TextBox is already contain specific text. A ComboBox show a certain value. How does it work?

Hier noch eine Beispieldatei / Here's a sample file:
UserForm, ComboBox, TextBox variabel ansprechen...[XLS 60 KB]

OptionExplicit
'-------------------------------------------------------------------------- 
' Module    : Module1 
' Procedure : Main 
' Author    : Case (Ralf Stolzenburg) 
' Date      : 22.02.2013 
' Purpose   : UserForms and TextBoxes to address variable... 
'-------------------------------------------------------------------------- 
PublicSub Main()
    Dim intCount2 AsInteger
    Dim intCount1 AsInteger
    Dim intCount AsInteger
    OnErrorGoTo Fin
    intCount2 = 1
    intCount1 = 2
    intCount = 3
    Call myForm("Userform"& intCount, "TextBox"& intCount1, _
        "ComboBox"& intCount2)
Fin:
    If Err.Number <> 0Then MsgBox "Error: "& _
        Err.Number &" "& Err.Description
EndSub
'-------------------------------------------------------------------------- 
' Module    : Module1 
' Procedure : myForm 
' Author    : Case (Ralf Stolzenburg) 
' Date      : 22.02.2013 
' Purpose   : UserForms and TextBoxes to address variable... 
'-------------------------------------------------------------------------- 
PrivateSub myForm(strForm AsString, strBox AsString, strCombo AsString)
    Dim blnCombo AsBoolean
    Dim blnForm AsBoolean
    Dim blnBox AsBoolean
    Dim intTMP AsInteger
    Dim objBox AsObject
    With ThisWorkbook.VBProject
        For intTMP = 1To .VBComponents.Count
            If .VBComponents.Item(intTMP).Type = 3Then
                If UCase$(.VBComponents.Item(intTMP).Name) = _
                    UCase$(strForm) Then
                    blnForm = True
                    ForEach objBox In .VBComponents(strForm). _
                        Designer.Controls
                        If objBox.Name = strBox Then
                            blnBox = True
                        ElseIf objBox.Name = strCombo Then
                            blnCombo = True
                        EndIf
                        If blnBox And blnCombo Then
                            .VBComponents(strForm). _
                                Designer.Controls(strBox).Text = "Test"
                            .VBComponents(strForm). _
                                Designer.Controls(strCombo).ListIndex = 3
                            UserForms.Add(strForm).Show
                            .VBComponents(strForm). _
                                Designer.Controls(strBox).Text = ""
                            ExitFor
                        EndIf
                    Next objBox
                EndIf
            EndIf
        Next intTMP
    EndWith
    IfNot blnForm Then blnBox = True: blnCombo = True: _
        MsgBox "UserForm does not exist!", vbCritical
    IfNot blnBox Then MsgBox "TextBox does not exist!", vbCritical
    IfNot blnCombo Then MsgBox "ComboBox does not exist!", vbCritical
EndSub

Viewing all articles
Browse latest Browse all 93