Combobox在另一个组合框内

时间:2017-06-05 03:37:25

标签: vba combobox word-vba

需要帮助编写代码 - 我已经开发了用户表单。

我需要一个组合框来显示一封信的主要部分: "根据我们的决定,HE / SHE有权获得退款" "根据我们的决定,HE / SHE无权获得退款"

但我还需要一个组合框来选择性别: " HE" " SHE"

到目前为止,我有:

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

有办法吗?

.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"

很高兴提供更多信息。

2 个答案:

答案 0 :(得分:1)

最简单的方法可能是填写“决定”。组合框每次都有适当的文字'性别'组合框变化。您可以通过捕获ComboBoxGender_Change事件来执行此操作,如下所示:

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 :(得分:1)

您可以在同一个组合框的不同列中存储多个选项。根据性别的选择,您可以隐藏其中一列或另一列。在代码中看起来如下。

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