英文:
How to Optionally filter on multiple columns using VBA
问题
以下是您提供的代码的翻译部分:
Sub ApplyFiltersFromTextBoxes()
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range
' 设置工作表和最后一行
Set ws = ThisWorkbook.Worksheets("PDF sheet")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' 根据TextBox3的输入应用过滤器(第4列)
Dim filterString As String
Dim filterValues4() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox3.Value)
If Len(filterString) > 0 Then
filterValues4 = Split(filterString, ",")
For i = LBound(filterValues4) To UBound(filterValues4)
filterValues4(i) = Trim(filterValues4(i))
Next i
Set rng = ws.Range("D1:D" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues4, Operator:=xlFilterValues
On Error GoTo 0
End If
' 根据TextBox4的输入应用过滤器(第5列)
Dim filterValues5() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox4.Value)
If Len(filterString) > 0 Then
filterValues5 = Split(filterString, ",")
For i = LBound(filterValues5) To UBound(filterValues5)
filterValues5(i) = Trim(filterValues5(i))
Next i
Set rng = ws.Range("E1:E" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues5, Operator:=xlFilterValues
On Error GoTo 0
End If
' 根据TextBox5的输入应用过滤器(第7列)
Dim filterValues7() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox5.Value)
If Len(filterString) > 0 Then
filterValues7 = Split(filterString, ",")
For i = LBound(filterValues7) To UBound(filterValues7)
filterValues7(i) = Trim(filterValues7(i))
Next i
Set rng = ws.Range("G1:G" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues7, Operator:=xlFilterValues
On Error GoTo 0
End If
' 根据TextBox6的输入应用过滤器(第8列)
Dim filterValues8() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox6.Value)
If Len(filterString) > 0 Then
filterValues8 = Split(filterString, ",")
For i = LBound(filterValues8) To UBound(filterValues8)
filterValues8(i) = Trim(filterValues8(i))
Next i
Set rng = ws.Range("H1:H" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues8, Operator:=xlFilterValues
On Error GoTo 0
End If
' 根据TextBox7的输入应用过滤器(第12列)
Dim filterValues12() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox7.Value)
If Len(filterString) > 0 Then
filterValues12 = Split(filterString, ",")
For i = LBound(filterValues12) To UBound(filterValues12)
filterValues12(i) = Trim(filterValues12(i))
Next i
Set rng = ws.Range("L1:L" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues12, Operator:=xlFilterValues
On Error GoTo 0
End If
End Sub
请注意,以上代码是用于从文本框中获取值并将其用作筛选条件的部分。希望这有助于您的项目!
英文:
So I have created a sheet with a large table of data and 19 columns and have created a userform that is used to generate the sheet with the required data and none of that has been an issue.
I have 5 particular columns (colum 4,5,7,8 and 12) that will be routinely used for filtering the data. As an attempt to incorporate this into the userform I created a ListBox for each column which displays the unique values from that column. An enduser can then multiselect the values they want to filter on from these columns which populate into TextBoxs (one for each ListBox).
Below is the code I have got so far for pulling the values from these text Boxes but when I run it all filters apply to the corresponding column of whichever textbox was populated first as opposed to their own columns. Not sure is this a limitation or is there anyway around it?
Also just to note it is not a requirement or neccesary for the filters to be used. Some cases will see multiple being used in different combinations and sometimes no filters might be used.
thanks in advance for any help:
Sub ApplyFiltersFromTextBoxes()
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range
' Set the worksheet and last row
Set ws = ThisWorkbook.Worksheets("PDF sheet")
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Apply filter based on the entries from TextBox3 (Column 4)
Dim filterString As String
Dim filterValues4() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox3.Value)
If Len(filterString) > 0 Then
filterValues4 = Split(filterString, ",")
For i = LBound(filterValues4) To UBound(filterValues4)
filterValues4(i) = Trim(filterValues4(i))
Next i
Set rng = ws.Range("D1:D" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues4, Operator:=xlFilterValues
On Error GoTo 0
End If
' Apply filter based on the entries from TextBox4 (Column 5)
Dim filterValues5() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox4.Value)
If Len(filterString) > 0 Then
filterValues5 = Split(filterString, ",")
For i = LBound(filterValues5) To UBound(filterValues5)
filterValues5(i) = Trim(filterValues5(i))
Next i
Set rng = ws.Range("E1:E" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues5, Operator:=xlFilterValues
On Error GoTo 0
End If
' Apply filter based on the entries from TextBox5 (Column 7)
Dim filterValues7() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox5.Value)
If Len(filterString) > 0 Then
filterValues7 = Split(filterString, ",")
For i = LBound(filterValues7) To UBound(filterValues7)
filterValues7(i) = Trim(filterValues7(i))
Next i
Set rng = ws.Range("G1:G" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues7, Operator:=xlFilterValues
On Error GoTo 0
End If
' Apply filter based on the entries from TextBox6 (Column 8)
Dim filterValues8() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox6.Value)
If Len(filterString) > 0 Then
filterValues8 = Split(filterString, ",")
For i = LBound(filterValues8) To UBound(filterValues8)
filterValues8(i) = Trim(filterValues8(i))
Next i
Set rng = ws.Range("H1:H" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues8, Operator:=xlFilterValues
On Error GoTo 0
End If
' Apply filter based on the entries from TextBox7 (Column 12)
Dim filterValues12() As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").TextBox7.Value)
If Len(filterString) > 0 Then
filterValues12 = Split(filterString, ",")
For i = LBound(filterValues12) To UBound(filterValues12)
filterValues12(i) = Trim(filterValues12(i))
Next i
Set rng = ws.Range("L1:L" & lastRow)
On Error Resume Next
rng.AutoFilter Field:=1, Criteria1:=filterValues12, Operator:=xlFilterValues
On Error GoTo 0
End If
End Sub
答案1
得分: 2
未经测试,但这应该接近正确。如SJR在评论中指出的 - 您需要筛选整个范围并提供每个列索引。
Sub ApplyFiltersFromTextBoxes()
Dim ws As Worksheet, rng As Range, data As Range
Set ws = ThisWorkbook.Worksheets("PDF sheet")
Set data = ws.Range("A1:S" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
FilterColumn data, 4, "TextBox3"
FilterColumn data, 5, "TextBox4"
FilterColumn data, 7, "TextBox5"
FilterColumn data, 8, "TextBox6"
FilterColumn data, 12, "TextBox7"
End Sub
Sub FilterColumn(rng As Range, fieldNum As Long, tbName As String)
Dim filterValues() As String, filterString As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").Controls(tbName).Value)
If Len(filterString) > 0 Then
filterValues = Split(filterString, ",")
For i = LBound(filterValues) To UBound(filterValues)
filterValues(i) = Trim(filterValues(i))
Next i
On Error Resume Next
rng.AutoFilter Field:=fieldNum, Criteria1:=filterValues, Operator:=xlFilterValues
On Error GoTo 0
End If
End Sub
英文:
Untested but this should be close. As SJR notes in the comments - you need to filter the whole range and supply each column index.
Sub ApplyFiltersFromTextBoxes()
Dim ws As Worksheet, rng As Range, data As Range
Set ws = ThisWorkbook.Worksheets("PDF sheet")
Set data = ws.Range("A1:S" & ws.Cells(ws.Rows.Count, "A").End(xlUp).row)
FilterColumn data, 4, "TextBox3"
FilterColumn data, 5, "TextBox4"
FilterColumn data, 7, "TextBox5"
FilterColumn data, 8, "TextBox6"
FilterColumn data, 12, "TextBox7"
End Sub
Sub FilterColumn(rng As Range, fieldNum As Long, tbName As String)
Dim filterValues() As String, filterString As String
filterString = Trim(TestVersionUpdate.MultiPage1.Pages("Page2").Controls(tbName).Value)
If Len(filterString) > 0 Then
filterValues = Split(filterString, ",")
For i = LBound(filterValues) To UBound(filterValues)
filterValues(i) = Trim(filterValues(i))
Next i
On Error Resume Next
rng.AutoFilter Field:=fieldNum, Criteria1:=filterValues, Operator:=xlFilterValues
On Error GoTo 0
End If
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论