Используйте ComboBox для фильтрации списка - PullRequest
0 голосов
/ 06 декабря 2018

Доброе утро всем!

Я в значительной степени новичок, но в то же время достаточно умел в адаптации кода (или забиваю свой путь до тех пор, пока мне не удастся как-то заставить работать!)

У меня возникли проблемыпроблема с получением списка пользовательских форм для фильтрации на основе выбора в выпадающем списке.Честно говоря, я даже не знаю, с чего начать эту работу ..

Мой код выглядит следующим образом:

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'

Поле со списком называется: «Список данных».Combo, который я хочу использовать для фильтрации данных, это «ComboBox7». Данные для списка сохраняются на «Sheet1». Я ищу возможность фильтровать в строке B данных на листе 1 (который является столбцом 2 списка)

Любая помощь будет принята с благодарностью !!

Если она поможет, я также могу опубликовать книгу.

Спасибо, Том

...