早上好!
我几乎是一个新手,但是相当擅长改编代码(或者在我设法以某种方式使事情正常之前大步向前!)
我无法让用户列表框根据组合框选择进行过滤。老实说,我什至不知道从哪里开始进行这项工作。
我的代码如下:
Option Explicit
Private Sub cmdClose_Click()
Unload UserForm1
End Sub
Private Sub ComboBox7_Change()
End Sub
Private Sub UserForm_Initialize()
cmdUpdate.Enabled = False 'Only enable the button when a row has been returned
'Combo Lists
ComboBox1.List = Array("COMPLETED", "HOLIDAY", "SICKNESS", "N/A")
ComboBox2.List = Array("COMPLETED", "HOLIDAY", "SICKNESS", "N/A")
ComboBox3.List = Array("COMPLETED", "HOLIDAY", "SICKNESS", "N/A")
ComboBox4.List = Array("COMPLETED", "HOLIDAY", "SICKNESS", "N/A")
ComboBox5.List = Array("COMPLETED", "HOLIDAY", "SICKNESS", "N/A")
ComboBox6.List = Array("COMPLETED", "HOLIDAY", "SICKNESS")
ComboBox7.List = Worksheets("Data").Range("A2:A50").Value
'Populate Listbox
Dim rng As Range
Dim i As Long, j As Long, rw As Long
Dim Myarray() As String
Set rng = Range("ListOfData")
With Me.ListOfData
.Clear
.ColumnHeads = False
.ColumnCount = rng.Columns.Count
ReDim Myarray(rng.Rows.Count, rng.Columns.Count)
rw = 0
For i = 1 To rng.Rows.Count
For j = 0 To rng.Columns.Count
Myarray(rw, j) = rng.Cells(i, j + 1)
Next
rw = rw + 1
Next
.List = Myarray
'.TopIndex = 1
End With
If Val(Me.txtLBSelectionIndex) > 1 Then
Me.ListOfData.Selected(Val(Me.txtLBSelectionIndex)) = True
End If
End Sub
Private Sub cmdSend_Click()
'NOT YET USED BUT WOULD WORK TO ADD NEW OPERATIVES
'Dim lastrow As Long
'lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
'Cells(lastrow + 1, "A").Value = txtIssue.Text
'Cells(lastrow + 1, "C").Value = txtDateReceived.Text
'Cells(lastrow + 1, "D").Value = txtAgency.Text
'Cells(lastrow + 1, "E").Value = txtService.Text
'Cells(lastrow + 1, "F").Value = txtSource.Text
'Cells(lastrow + 1, "G").Value = txtIssueType.Text
'Cells(lastrow + 1, "H").Value = txtIssueNonIssue.Text
'Cells(lastrow + 1, "I").Value = txtOwnership.Text
'Cells(lastrow + 1, "J").Value = txtTimeSpent.Text
'Cells(lastrow + 1, "K").Value = txtDateCompleted.Text
'Cells(lastrow + 1, "L").Value = txtActiveDuration.Text
End Sub
Private Sub ListOfData_Change()
End Sub
Private Sub ListofData_Click()
Dim rngMyData As Range
txtIssue.Value = Me.ListOfData.Column(0)
ComboBox1.Value = Me.ListOfData.Column(2)
ComboBox2.Value = Me.ListOfData.Column(3)
ComboBox3.Value = Me.ListOfData.Column(4)
ComboBox4.Value = Me.ListOfData.Column(5)
ComboBox5.Value = Me.ListOfData.Column(6)
ComboBox6.Value = Me.ListOfData.Column(7)
TextBox1.Value = Me.ListOfData.Column(8)
TextBox2.Value = Me.ListOfData.Column(9)
txtDateCompleted.Value = Me.ListOfData.Column(10)
txtActiveDuration.Value = Me.ListOfData.Column(11)
Set rngMyData = Sheets("Sheet1").Columns("A")
On Error Resume Next
txtRowNumber = Application.WorksheetFunction.Match(txtIssue.Value,
rngMyData, 0)
On Error Resume Next
If Val(txtRowNumber) > 1 Then 'Exclude the ability to change the header row.
cmdUpdate.Enabled = True 'OK to enable the button as an applicable row
number has been returned
End If
End Sub
Private Sub cmdUpdate_Click()
Dim lngMyRow As Long
Dim r As Long
lngMyRow = Val(txtRowNumber)
If lngMyRow = 0 Then
MsgBox "Update is not available as a row number for the selected issue
could not be found.", vbExclamation
Exit Sub
Else
Application.EnableEvents = False
'Return the selected index number of the selected record in the
'ListOfDetails' listbox so it can be re-selected after the list has been
refreshed
For r = 0 To Me.ListOfData.ListCount - 1
If Me.ListOfData.Selected(r) Then
Me.txtLBSelectionIndex = r
Exit For
End If
Next r
'Populate the corresponding cells with any change
Worksheets("Sheet1").Cells(lngMyRow, "A").Value = txtIssue.Text
Worksheets("Sheet1").Cells(lngMyRow, "C").Value = ComboBox1.Text
Worksheets("Sheet1").Cells(lngMyRow, "D").Value = ComboBox2.Text
Worksheets("Sheet1").Cells(lngMyRow, "E").Value = ComboBox3.Text
Worksheets("Sheet1").Cells(lngMyRow, "F").Value = ComboBox4.Text
Worksheets("Sheet1").Cells(lngMyRow, "G").Value = ComboBox5.Text
Worksheets("Sheet1").Cells(lngMyRow, "H").Value = ComboBox6.Text
'Cells(lngMyRow, "I").Value = TextBox1.Text - Not running on update
to stop override of PDR Due Yes/No
Worksheets("Sheet1").Cells(lngMyRow, "J").Value =
Format(TextBox2.Value, "mm/dd/yyyy")
'Cells(lngMyRow, "K").Value = txtDateCompleted.Text
'Cells(lngMyRow, "L").Value = txtActiveDuration.Text
Application.EnableEvents = True
End If
'Refresh the list
'Me.ListOfData.RowSource = "ListOfData"
Call UserForm_Initialize
End Sub'
该列表框称为:“数据列表” 我要用来过滤数据的组合是“ ComboBox7” 列表框的数据存储在“ Sheet1”上 我希望能够在工作表1(列表框的第2列)中的数据的行B上进行过滤
任何帮助将不胜感激!
如果有帮助,我也可以发布工作簿。
谢谢 汤姆