Excel VBA 多个不区分大小写的“包含”条件筛选

huangapple go评论120阅读模式
英文:

Excel VBA filter for multiple case independent "Contains" criteria

问题

我需要过滤列E(该列包含文本值),以便筛选出包含列表中任何值("ALL","SUPER","EXTRA")的值,筛选应不区分大小写。例如,筛选应保留"Fall","fAll","FaLL"(不区分大小写的"ALL"),以及"Dextra"。某种方式,我无法使用UI完成它。逐行检查instring是最后的选择,但如果使用VBA筛选,将更适合目前的工作。非常感谢任何帮助。

英文:

I need to filter column E (column has text values) for any value that contains any of the values from the list ("ALL", "SUPER", "EXTRA") and the filter should be case independent. For example filter should retain "Fall", "fAll", "FaLL" (case independent for "ALL") and "Dextra".

Somehow I could not do it with UI. Row by row checking with instring is the last option but if done by VBA filter it would better suit the present work. Any help greatly appreciated.

答案1

得分: 1

以下是您要翻译的代码部分:

  1. Sub filterByPartialStrins()
  2. Dim sh As Worksheet, colMark As Range, lastR As Long, lastC As Long
  3. Dim arrCrit(), arr, arrMark, El, i As Long
  4. Const markName As String = "Marker_column"
  5. arrCrit = Array("ALL", "SUPER", "EXTRA") 'place partial criteria in an array
  6. Set sh = ActiveSheet 'use here the sheet you need
  7. If sh.AutoFilterMode Then sh.AutoFilterMode = False 'eliminate the filter, if any
  8. lastR = sh.Range("E" & sh.Rows.Count).End(xlUp).Row 'last row
  9. 'determine the column where the marker to be placed:
  10. Set colMark = sh.Rows(1).Find(What:=markName, LookIn:=xlValues, LookAt:=xlWhole)
  11. If Not colMark Is Nothing Then
  12. lastC = colMark.Column 'for the case when the marker column exists
  13. Else
  14. lastC = sh.Cells(1, sh.Columns.Count).End(xlToLeft).Column + 1 'next empty column if marker column does not exist
  15. End If
  16. arr = sh.Range("E2:E" & lastR).Value2 'place E:E column in an array for faster processing
  17. ReDim arrMark(1 To UBound(arr), 1 To 1) 'ReDim the array to keep markers
  18. For i = 1 To UBound(arr)
  19. For Each El In arrCrit
  20. 'place a marker if the necessary condition is true:
  21. If InStr(1, arr(i, 1), El, vbTextCompare) > 0 Then arrMark(i, 1) = "OK": Exit For
  22. Next El
  23. Next
  24. If colMark Is Nothing Then sh.Cells(1, lastC).Value = markName 'place the marker column name, IF NOT EXISTS
  25. 'drop the arrMark content, at once:
  26. sh.Cells(2, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
  27. 'filter by the marker column
  28. sh.Range("A1").CurrentRegion.AutoFilter lastC, "OK"
  29. MsgBox "Ready..."
  30. End Sub

希望这有所帮助!

英文:

Please, test the next code, which puts in practice my sugestions from the above comment. It should be fast enough, even for a large range. All processing is done in memory, only the markers array is dropped at once, at the end of the code, followed by filtering on it. This solution assumes that the headers exist in the sheet first row:

  1. Sub filterByPartialStrins()
  2. Dim sh As Worksheet, colMark As Range, lastR As Long, lastC As Long
  3. Dim arrCrit(), arr, arrMark, El, i As Long
  4. Const markName As String = "Marker_column"
  5. arrCrit = Array("ALL", "SUPER", "EXTRA") 'place partial criteria in an array
  6. Set sh = ActiveSheet 'use here the sheet you need
  7. If sh.AutoFilterMode Then sh.AutoFilterMode = False 'eliminate the filter, if any
  8. lastR = sh.Range("E" & sh.rows.count).End(xlUp).Row 'last row
  9. 'determinte the column where the marker to be placed:
  10. Set colMark = sh.rows(1).Find(What:=markName, LookIn:=xlValues, Lookat:=xlWhole)
  11. If Not colMark Is Nothing Then
  12. lastC = colMark.column 'for the case when the marker column exists
  13. Else
  14. lastC = sh.cells(1, sh.Columns.count).End(xlToLeft).column + 1 'next empty column if marker column does not exist
  15. End If
  16. arr = sh.Range("E2:E" & lastR).Value2 'place E:E column in an array for faster processing
  17. ReDim arrMark(1 To UBound(arr), 1 To 1) 'ReDim the array to keep markers
  18. For i = 1 To UBound(arr)
  19. For Each El In arrCrit
  20. 'place a marker if the necessary condition is true:
  21. If InStr(1, arr(i, 1), El, vbTextCompare) > 0 Then arrMark(i, 1) = "OK": Exit For
  22. Next El
  23. Next
  24. If colMark Is Nothing Then sh.cells(1, lastC).Value = markName 'place the marker column name, IF NOT EXISTS
  25. 'dropo the arrMark content, at once:
  26. sh.cells(2, lastC).Resize(UBound(arrMark), 1).Value2 = arrMark
  27. 'filter by the marker column
  28. sh.Range("A1").CurrentRegion.AutoFilter lastC, "OK"
  29. MsgBox "Ready..."
  30. End Sub

huangapple
  • 本文由 发表于 2023年3月1日 16:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定