我如何创建一个在表单框和两个复选框选项中返回名称的用户窗体?

问题描述 投票:0回答:1

我正在尝试创建一个用户表单,该表单从列表框中返回名称,并为从列表框中选择的名称返回相应的“天”和“金额”(名称,金额和金额均为电子表格上的所有范围)。我是VBA的新手,因此无法设置代码。是否有人对如何设置代码有任何指示?我的用户表单和电子表格附在下面,感谢您的帮助!

到目前为止,我的代码是:

Private Sub BtnCancel_Click()
MsgBox ("Report Cancelled")
Unload FrmCust
End Sub



Private Sub BtnOk_Click()




' msgbox showing the user's selection, as well 
' as days and amount if selected in the checkboxes

End Sub

Private Sub UserForm_Initialize()

' Fill the listbox with each user's name

   Dim cell As Range

'Load to ListBox

  FrmCust.ListBoxCustlist.MultiSelect = fmMultiSelectSingle 'allows only one selection

  For Each cell In Worksheets("Data").Range("J3:j17")
    ListBoxCustlist.AddItem cell.Value
  Next cell



End Sub

用户表单和相关电子表格

enter image description here

excel vba userform
1个回答
0
投票

代码中的注释表明,您只希望某些MsgBox代码显示所选的值。 (您没有包括复选框控件的名称,所以我分别使用了Checkbox1和Checkbox2。修改它们以匹配复选框控件的名称。)

If ListBoxCustlist.ListIndex = -1 Then
  MsgBox "No Item was selected"
Else
  MsgBox ListBoxCustlist.List(ListBoxCustlist.ListIndex, 0) & " - Days=" & CheckBox1.Value & " - Amount=" & CheckBox2.Value
  ' Me.Hide ' Optionally hide the form
End If
© www.soinside.com 2019 - 2024. All rights reserved.