我这里有一个列表框,可以与下面的代码一起正常工作。
列表框1
更新了整个代码:
Option Explicit
Dim bInit As Boolean ' ** module-scoped variable
Private Sub UserForm_Initialize()
bInit = True ' ** set UserForm_Initialize mode **from Taller
clearAll
Me.cmbName.Value = "Nory"
Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
Dim i As Long
Dim arr: arr = ws.Range("B1").CurrentRegion.Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(arr)
dict(arr(i, 2)) = arr(i, 1)
Next
' ***
If dict.exists(Me.cmbName.Value) Then
Me.cmbTeam.Value = dict(Me.cmbName.Value)
If Not cmbTeam.Value = "" And Not cmbDate.Value = "" And Not cmbName.Value = "" Then
Team
forListBoxUpdate 'calling forListBoxUpdate
forDateCombobox 'filling in date dropdowns
forNamesCombobox 'filling in names combobox as long as 3 comboboxes are not blank
Else
Team 'calling Team dropdowns in case team is blank or default cmbName value has no team
cmbDate.Value = ""
cmbName.Value = ""
End If
Else
Team 'calling Team dropdowns in case team is blank or default cmbName value has no team
cmbDate.Value = ""
cmbName.Value = ""
End If
bInit = False ' ** reset **from Taller
End Sub
Private Sub cmbDate_Change()
If Not cmbTeam = "" And Not cmbName = "" Then
forListBoxUpdate 'calling to show info if team and name not blank
forNamesCombobox 'filling in cmbname dropdowns
Else
cmbDate.Clear 'if cmbteam and cmbname blank, cmdate should also be blank
End If
End Sub
Private Sub cmbName_Change()
forListBoxUpdate
End Sub
Private Sub cmbTeam_Change()
cmbDate.Value = ""
'cmbName.Value = "" 'issue, during initialize, default cmbname is removed.
clearAll 'used this instead
forDateCombobox 'fills in date dropdowns
End Sub
Sub forListBoxUpdate() 'to show info from sheets and to be called after the 3 comboboxes are filled
Dim ws As Worksheet, colList As Collection
Dim arrData, arrList, i As Long, j As Long
Set colList = New Collection
Set ws = Worksheets("Sheet2")
arrData = ws.Range("A1:C" & ws.Cells(ws.Rows.count, "A").End(xlUp).Row)
For i = 2 To UBound(arrData)
If Format(arrData(i, 1), "mmmm yyyy") = Me.cmbDate.Value And arrData(i, 3) = Me.cmbName.Value Then
colList.Add i, CStr(i)
End If
Next
ReDim arrList(1 To colList.count + 1, 1 To UBound(arrData))
For j = 1 To 3
arrList(1, j) = arrData(1, j) ' header
For i = 1 To colList.count
arrList(i + 1, j) = arrData(colList(i), j)
Next
Next
With Me.ListBox1
.Clear
.ColumnCount = UBound(arrData, 2)
.list = arrList
End With
labelCount.Caption = ListBox1.ListCount - 1
End Sub
Sub clearAll() 'to clear comboboxes except teams
If Not bInit Then cmbName.Value = "" ' ** doesn't run when calling from UserForm_Initialize **from Taller
cmbDate.Clear
cmbName.Clear
'cmbName.Value = ""
ListBox1.Clear
End Sub
Sub Team() 'for adding the teams dropdown in cmbTeam
clearAll
Dim ws As Worksheet, _
Dic As Object, _
rCell As Range, _
Key
Set ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
For Each rCell In ws.Range("A2", ws.Cells(Rows.count, "A").End(xlUp))
If Not Dic.exists(rCell.Value) And Not rCell = "" Then
Dic.Add rCell.Value, Nothing
End If
Next rCell
For Each Key In Dic
cmbTeam.AddItem Key
Next
End Sub
Sub forNamesCombobox() 'for adding the names dropdown in cmbName
Dim ws As Worksheet, _
Dic As Object, _
rCell As Range, _
Key
Set ws = Worksheets("Sheet1")
Set Dic = CreateObject("Scripting.Dictionary")
For Each rCell In ws.Range("B2", ws.Cells(Rows.count, "B").End(xlUp))
If Not Dic.exists(rCell.Value) And rCell.Offset(0, -1) = cmbTeam.Value Then
Dic.Add rCell.Value, Nothing
End If
Next rCell
For Each Key In Dic
cmbName.AddItem Key
Next
End Sub
Sub forDateCombobox() 'for adding the date dropdown in cmbDate
Dim date1 As Variant
Dim date2 As Variant
date1 = Format(Now, "mmmm yyyy")
date2 = Format(DateAdd("m", -1, CDate(date1)), "mmmm yyyy")
With cmbDate
.Clear
.AddItem Format(date2, "mmmm yyyy")
.AddItem Format(date1, "mmmm yyyy")
.Value = Format(date1, "mmmm yyyy")
End With
End Sub
工作表1
一个 | B |
---|---|
团队 | 名字 |
鹦鹉 | 莉娜 |
乔治 | 诺里 |
乔治 | 最大限度 |
杰克 | 担 |
工作表2
一个 | B | 碳 |
---|---|---|
日期 | ID | 名字 |
2025年3月25日 | 1101 | 莉娜 |
2025年4月25日 | 1102 | 莉娜 |
2025年3月25日 | 1103 | 诺里 |
2025年4月25日 | 1104 | 诺里 |
2025年3月25日 | 1105 | 担 |
2025年4月25日 | 1106 | 担 |
现在,在团队组合框的更改事件期间,我希望清除名称组合框(“Nory”或任何值cmbName
都应被删除并消失)。
从上面的代码中,团队变更事件片段是:
Private Sub cmbTeam_Change()
cmbDate.Value = ""
'cmbName.Value = "" 'issue, during initialize, default cmbname is removed.
clearAll 'used this instead
forDateCombobox 'fills in date dropdowns
End Sub
Sub clearAll() 'to clear comboboxes except teams
If Not bInit Then cmbName.Value = "" ' ** doesn't run when calling from UserForm_Initialize **from Taller
cmbDate.Clear
cmbName.Clear
'cmbName.Value = ""
ListBox1.Clear
End Sub
即使我将在初始化期间插入cmbName.Value = ""
到 sub 中clearAll
,"Nory"
也会被删除,但我不希望它在初始化时被删除。
如何修复上述代码,在初始化期间,“Nory”将保留,团队和日期也将保留,而当团队发生变化时,日期和姓名组合框将为空白。
非常感谢您的帮助。