Combobox在另一个组合框内
问题描述:
需要帮助编写代码 - 我已经开发了一个用户窗体。Combobox在另一个组合框内
我需要一个组合框来显示一个字母的主要部分: “根据我们的决心,他/她有权要求退款”,“基于我们的决心,他/她不享有退款”
,但我也需要一个组合框中选择性别: “他” “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"
乐意提供更多信息。
答
最简单的方法可能是每当'Gender'组合框发生变化时用适当的文本填充'Decision'组合框。你会通过捕获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
答
可以存储在同一个组合框的不同列中多个选择。根据性别的选择,然后隐藏一列或另一列。代码如下所示。
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