列表框取决于两个组合框VB​​A

时间:2017-12-14 19:59:21

标签: vba combobox listbox dependencies

我试图根据我选择的前两个组合框制作三个列表框列。

以下是我当前表单的截图:

enter image description here

我尝试过做过例,但这只是自己使用两个组合框而不会说

有没有办法做到这一点?

目前我有这个:

Private Sub UserForm_Initialize()

With StateComboBox


    .AddItem "CO - Colorado"

End With
End Sub
____________________________________________________
Private Sub StateComboBox_Change()

Dim index As Long
index = StateComboBox.ListIndex

RoleComboBox.Clear

Select Case StateComboBox.Value
    Case Is = "CO - Colorado"
     With RoleComboBox
        .AddItem "District"
        .AddItem "School"
        .AddItem "Test Administrator"
    End With


End Select
End Sub
____________________________________________
Private Sub PermissionSetList_change()

If StateComboBox.Value = "CO - Colorado" And RoleComboBox.Value = "District" Then
Call ColoradoDPermissionSet
Call ColoradoDRemovePermissions
Call ColoradoDAddPermissions
End If

End Sub
____________________________________________
Sub ColoradoDPermissionSet()

With PermissionSetList

.AddItem "Administrator"
.AddItem "Correction Primary Window"
.AddItem "Documents - View"

End With
End Sub
________________________________________________________
Sub ColoradoDRemovePermissions()

With RemoveList

.AddItem "Corrections - Primary"
.AddItem "Student Transfer Form"

End With
End Sub
_________________________________________________
Sub ColoradoDAddPermissions()

With AddList

.AddItem "Test Tickets - End Incomplete Test"
.AddItem "Test Tickets - Invalidate"
.AddItem "Test Tickets - Regenerate"

End With
End Sub

我已在代码中放入IF语句,当我选择CO - Colorado和District时,列表框将不会填充。我想拥有它,当我选择状态和角色时,它将填充列表框中的特定值,当我更改状态和/或角色时,它将提供一组不同的“权限”,这就是为什么我放置在子内部列出并调用它们。

1 个答案:

答案 0 :(得分:0)

我已经想到了这一点,而不是单独使用If语句,我做了以下操作,并填写了我想要的方式:

Private Sub RoleComboBox_Change()

Dim index As Long
index = RoleComboBox.ListIndex

PermissionSetList.Clear
RemoveList.Clear
AddList.Clear

If StateComboBox.Value = "CO - Colorado" Then
  Select Case RoleComboBox.Value
    Case Is = "District"
      With PermissionSetList
        Call ColoradoDPermissionSet
        Call ColoradoDRemovePermissions
        Call ColoradoDAddPermissions
      End With
 End Select
End If
End Sub

这是可扩展的,因此添加更多If可以允许更多选择:

https://i.imgur.com/lkGDrLs.png

Private Sub RoleComboBox_Change()

Dim index As Long
index = RoleComboBox.ListIndex

PermissionSetList.Clear
RemoveList.Clear
AddList.Clear

If StateComboBox.Value = "AL - Alabama" Then
With PermissionSetList
 Select Case RoleComboBox.Value

 Case Is = "District"
     Call WIDADPermissionSet
     Call AlabamaDRemovePermissions
     Call AlabamaDAddPermissions

  Case Is = "School"
     Call AlabamaSPermissionSet
     Call AlabamaSRemovePermissions
      Call AlabamaSAddPermissions

 Case Is = "Test Administrator"
     Call WIDATPermissionSet
     Call WIDATRemovePermissions
     Call WIDATAddPermissions
   End Select
 End With
End If

If StateComboBox.Value = "CO - Colorado" Then
  With PermissionSetList
    Select Case RoleComboBox.Value

   Case Is = "District"
       Call WIDADPermissionSet
       Call ColoradoDRemovePermissions
       Call ColoradoDAddPermissions

   Case Is = "School"
       Call ColoradoSPermissionSet
       Call ColoradoSRemovePermissions
       Call ColoradoSAddPermissions

   Case Is = "Test Administrator"
       Call WIDATPermissionSet
       Call WIDATRemovePermissions
       Call WIDATAddPermissions
    End Select
  End With
End If


End Sub