Excel用户窗体将多行添加到工作表-依赖组合框?

时间:2018-08-28 09:29:53

标签: excel vba excel-vba combobox userform

我需要一个用户窗体中的帮助才能在Sheet1上添加多个行。看起来像这样(控件名称为红色):Userform with control names in red

这是我的代码:

Dim cNum As Integer
Dim X As Integer
Dim nextrow As Range
Dim rRef As String
Dim ctrl As MSForms.Control

Private Sub UserForm_Initialize()
For Each ctrl In Controls
If ctrl.Name Like "*rec4" Then ctrl = Date
If TypeOf ctrl Is MSForms.ComboBox Then
If ctrl.Name Like "*rec1" Then ctrl.List = Array("Company1", "Company2", "Company3")
If ctrl.Name Like "*rec2" Then ctrl.List = Array("Supplier1", "Supplier2", "Supplier3")
If ctrl.Name Like "*rec5" Then ctrl.List = Array("Wrong Order", "Wrong Company", "Wrong Charge","Internal Deletion")
End If
Next
End Sub

Private Sub Arec5_Change()
Select Case Arec5.Value
Case Is = "Wrong Order"
Arec6.List = Array("Linked to other supplier", "Not Valid", "Missing figure")
Case Is = "Wrong Company"
Arec6.List = Array("Company1", "Company2", "Company3")
Case Is = "Wrong Charge"
Arec6.List = Array("Recupel", "Bebat", "Valorlub")
Case Is = "Internal Deletion"
Arec6.List = Array("Already Handled", "Manual Handling", "Request", "Deleted order")
End Select
End Sub

Private Sub btnClear_Click()
Unload Me
UserForm1.Show
End Sub

Private Sub btnClose_Click()
Unload Me
End Sub

Private Sub btnSubmit_Click()
cNum = 7
rRef = "Arec"
Addme
rRef = "Brec"
Addme
rRef = "Crec"
Addme
rRef = "Drec"
Addme
rRef = "Erec"
Addme
rRef = "Frec"
Addme
rRef = "Grec"
Addme
rRef = "Hrec"
Addme
rRef = "Irec"
Addme
rRef = "Jrec"
Addme
rRef = "Lrec"
Addme
rRef = "Mrec"
Addme
rRef = "Nrec"
Addme
rRef = "Orec"
Addme
rRef = "Prec"
Addme
rRef = "Qrec"
Addme
rRef = "Rrec"
Addme
rRef = "Srec"
Addme
End Sub

Sub Addme()
Set nextrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
For X = 1 To cNum
nextrow = Me.Controls(rRef & X).Value
Set nextrow = nextrow.Offset(0, 1)
Next
End Sub

我想做的是:

将相同的更改事件(Arec5_Change)应用于所有*rec5组合框,以便每个*rec6组合框都依赖于左侧的*rec5组合框。

如何实现?

下一步,我还要在*rec5中找到“内部删除”时将行发送到Sheet2。

有可能吗?

1 个答案:

答案 0 :(得分:0)

根据评论-您已经说过,现在,您将对19个控件的事件处理程序进行硬编码。但是,为了将来参考(以及进一步了解VBA),可以创建一个 class 事件处理程序以分配给多个控件对象,包括在运行时在UserForm上创建的那些控件。

查看以下链接: