我正在尝试创建一个用户表单,该表单从列表框中返回名称,并为从列表框中选择的名称返回相应的“天”和“金额”(名称,金额和金额均为电子表格上的所有范围)。我是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
用户表单和相关电子表格
代码中的注释表明,您只希望某些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