[英]How to check for empty cells in range if empty then check in selection VBA?
我有以下代码,将通过用户selection
过滤表。
我想首先检查range A3:T3
如果它们有值,如果它们,我将添加到我的阵列并开始过滤并忽略Range A3:T3
所有空单元格Range A3:T3
。
如果范围A3:T3为空,则按用户selection
进行过滤。
我注意到有一个错误,如果我过滤一个单元格,并且该单元格有2行数据1将显示而1不是因为前面有空格我认为它的ChrW
是否有任何方法使过滤器采取部分匹配包括两个版本? 我知道我可以替换ChrW
并替换它,但是数据将始终从outlook粘贴,因此发生的频率ChrW
,所以如果我可以进行部分匹配,这可能会解决很多问题。
任何帮助将非常感激。
Sub advancedMultipleCriteriaFilter()
Dim cellRng As Range, tableObject As Range, subSelection As Range
Dim filterCriteria() As String, filterFields() As Integer
Dim i As Integer
Application.ScreenUpdating = False
'Call removeSpace
If Selection.Rows.Count > 1 Then
MsgBox "Cannot apply filter to multiple rows within the same column. Please make another selection and try again.", vbInformation, "Selection Error!"
Exit Sub
End If
i = 1
ReDim filterCriteria(1 To Selection.Cells.Count) As String
ReDim filterFields(1 To Selection.Cells.Count) As Integer
Set tableObject = Selection.CurrentRegion
For Each subSelection In Selection.Areas
For Each cellRng In subSelection
filterCriteria(i) = cellRng.Text
filterFields(i) = cellRng.Column - tableObject.Cells(1, 1).Column + 1
i = i + 1
Next cellRng
Next subSelection
With tableObject
For i = 1 To UBound(filterCriteria)
.AutoFilter field:=filterFields(i), Criteria1:=filterCriteria(i)
Next i
End With
Call GetLastRow
Set tableObject = Nothing
Application.ScreenUpdating = True
End Sub
Sub resetFilters()
Dim sht As Worksheet
Dim LastRow As Range
Application.ScreenUpdating = False
On Error Resume Next
If ActiveSheet.FilterMode Then
ActiveSheet.ShowAllData
End If
Range("A3:T3").ClearContents
Application.ScreenUpdating = True
Call GetLastRow
End Sub
Private Sub GetLastRow()
'Step 1: Declare Your Variables.
Dim LastRow As Long
'Step 2: Capture the last used row number.
LastRow = Cells(Rows.Count, 8).End(xlUp).Row
'Step 3: Select the next row down
Cells(LastRow, 8).Offset(1, 0).Select
End Sub
如果要在单个字段中过滤多个条件,则需要如下数组:
.AutoFilter Field:=1, Criteria1:=Array("val 2", "val 3", "val 4"), Operator:=xlFilterValues
所以filterCriteria(i)必须是一个数组,你可以这样做:
filterCriteria(i) = Array("item1","item2")
.AutoFilter Field:=filterFields(i), Criteria1:=filterCriteria(i), Operator:=xlFilterValues
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.