2017-06-05 1 views
0

Besoin d'aide pour écrire un code - J'ai développé un userform.Combobox dans un autre combobox

J'ai besoin d'une zone de liste déroulante pour afficher la section principale d'une lettre: « basée sur notre détermination, il/elle a droit à un remboursement » « basée sur notre détermination, il/elle n'a pas droit à un remboursement »

mais je aussi besoin d'une zone de liste déroulante pour sélectionner le genre: « HE » « ELLE »

jusqu'à présent j'ai:

Private Sub Userform_Initialize() 
With ComboBoxDecision 
.AddItem "based on our determination HE/SHE is entitled to a refund" 
.AddItem "based on our determination HE/SHE is not entitled to a refund" 
End With 
With ComboBoxGender 
.AddItem "HE" 
.AddItem "SHE" 
End With 
lbl_exit: 
Exit sub 
End Sub 

Private Sub CommandButtonOk_Click() 
Application.ScreenUpdating = False 
With ActiveDocument 
    .Bookmarks("Decision").Range.Text = ComboBoxDecision.Value 
    End With 
Application.ScreenUpdating = True 
Unload Me 
End Sub 

Est-il possible de le faire:

.AddItem "based on our determination "Whatever option was selected in ComboBoxGender" is entitled to a refund" 
.AddItem "based on our determination "Whatever option was selected in ComboBoxGender" is not entitled to a refund" 

Heureux de fournir plus d'informations.

Répondre

1

La manière la plus simple serait probablement de remplir la liste déroulante 'Décision' avec le texte approprié chaque fois que la liste déroulante 'Sexe' change. Vous feriez cela en piégeant l'événement ComboBoxGender_Change, comme ceci:

Private Sub ComboBoxGender_Change() 
    Dim gndr As String 

    gndr = Me.ComboBoxGender.Text 

    With Me.ComboBoxDecision 
     .Clear 
     .AddItem "based on our determination " & _ 
       gndr & " is entitled to a refund" 
     .AddItem "based on our determination " & _ 
       gndr & " is not entitled to a refund" 
    End With 

End Sub 

Private Sub UserForm_Initialize() 
    With Me.ComboBoxGender 
     .AddItem "HE" 
     .AddItem "SHE" 
     .ListIndex = 0 
    End With 
End Sub 
1

Vous pouvez stocker plusieurs choix dans différentes colonnes de la même zone de liste déroulante. Selon le choix du genre, vous cachez alors l'une ou l'autre des colonnes. Dans le code qui aurait l'air comme suit.

Option Explicit 
Option Base 0 

Private Sub UserForm_Initialize() 

    Dim Arr(1, 1) As String 

    ' Column, item: 
    Arr(1, 0) = "Based on our determination she is not entitled to a refund" 
    Arr(0, 0) = Replace(Arr(1, 0), "not", "") 
    Arr(1, 1) = Replace(Arr(1, 0), "she", "he") 
    Arr(0, 1) = Replace(Arr(1, 1), "not", "") 
    With CbxDecision 
     .List = Arr 
     .ColumnCount = 2 
     .ListIndex = 0 
    End With 

    With CbxGender 
     .List = Split("He She") 
     .ListIndex = 0 
    End With 
End Sub 


Private Sub CbxGender_Change() 
    ' set the list width for 1st and 2nd column 
    ' one of them must always be 0 (= hidden) 
    CbxDecision.ColumnWidths = Split("0pt;90pt|90pt;0pt", "|")(CbxGender.ListIndex) 
End Sub