2016-10-08 85 views
0

表单将接受信息并将信息复制到工作表1。 表单包含5个文本框和2个组合框。第一个combobox选项是CRIS,TRACS和DOCS。第二个组合框选项应该基于第一个组合框选择。基于上一个组合框的Excel组合框下拉项目

Form Design

这里是我到目前为止的代码:

Private Sub cmdClear_Click() 
Call UserForm_Initialize 
End Sub 

Private Sub cmdMove_Click() 

Dim emptyRow As Long 

Sheet1.Activate  'Make Sheet1 active 

emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1  

'Transfer information 
Cells(emptyRow, 1).Value = txtName.Value 
Cells(emptyRow, 2).Value = txtBtn.Value 
Cells(emptyRow, 3).Value = txtCbr.Value 
Cells(emptyRow, 4).Value = txtOrder.Value 
Cells(emptyRow, 5).Value = txtTrouble.Value 
Cells(emptyRow, 6).Value = ComboBox1.Value 

End Sub 
Private Sub UserForm_Click() 

End Sub 

Private Sub UserForm_Initialize() 

txtName.Value = ""   'Empty NameTextBox 
txtBtn.Value = ""   'Empty BTN 
txtCbr.Value = ""   'Empty CBR 
txtOrder.Value = ""   'Empty Order Number 
txtTrouble.Value = ""  'Empty Trouble Ticket Number 

ComboBox1.Clear 

With ComboBox1 
    .AddItem "CRIS" 
    .AddItem "TRACS" 
    .AddItem "DOCS" 
End With 

txtName.SetFocus 

End Sub 
+0

喜先生。也感谢您的快速响应。在我的截图中,我们可以在第二个组合框中找到要填充的数据。 – Zhamepace

回答

1

在您的用户窗体的代码窗格中添加以下代码:

Private Sub ComboBox1_Change() 
    With Me 
     If .ComboBox1.ListIndex <> -1 Then 
      Select Case .ComboBox1.Value 
       Case "CRIS" 
        .ComboBox2.List = Array("close", "reroute", "transfer") 
       Case "TRACS" 
        .ComboBox2.List = Array("close", "reroute") 
       Case "DOCS" 
        .ComboBox2.List = Array("completed", "transfer", "update") 
      End Select 
     End If 
    End With 
End Sub 
+0

感谢user3598756的快速响应。它现在效果更好。 – Zhamepace

+0

感谢它的帮助。我也从朋友那里得到这段代码,它看起来很相似。 – Zhamepace

+0

不客气。然后请将我的答案标记为已接受。谢谢 – user3598756