简体   繁体   English

如果为空,如何检查范围内的空单元格然后检入选择VBA?

[英]How to check for empty cells in range if empty then check in selection VBA?

I have the below code that will filter the table by the user selection . 我有以下代码,将通过用户selection过滤表。

  1. I would like to first check range A3:T3 if they have values in and if they do i will add to my array and start filtering by that and ignore all empty cells in Range A3:T3 . 我想首先检查range A3:T3如果它们有值,如果它们,我将添加到我的阵列并开始过滤并忽略Range A3:T3所有空单元格Range A3:T3

  2. If range A3:T3 is empty then filter by user selection . 如果范围A3:T3为空,则按用户selection进行过滤。

  3. I notice there is a bug where if i filter by a cell and that cell has 2 rows with data 1 will show and 1 not because there is a space in front of it i think its a ChrW is there any way to make the filter take partial match to include both versions? 我注意到有一个错误,如果我过滤一个单元格,并且该单元格有2行数据1将显示而1不是因为前面有空格我认为它的ChrW是否有任何方法使过滤器采取部分匹配包括两个版本? I know i can replace the ChrW and replace it but data will be always pasted from outlook so the occurrence will be frequent so if i can take a partial match that will probably solve a lot of the problems. 我知道我可以替换ChrW并替换它,但是数据将始终从outlook粘贴,因此发生的频率ChrW ,所以如果我可以进行部分匹配,这可能会解决很多问题。

Any help would be much appreciated. 任何帮助将非常感激。

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

If you want to filter multi-criteria in a single field, you will need array as below: 如果要在单个字段中过滤多个条件,则需要如下数组:

.AutoFilter Field:=1, Criteria1:=Array("val 2", "val 3", "val 4"), Operator:=xlFilterValues

so filterCriteria(i) must be an Array, you can just do it as below: 所以filterCriteria(i)必须是一个数组,你可以这样做:

filterCriteria(i) = Array("item1","item2")
.AutoFilter Field:=filterFields(i), Criteria1:=filterCriteria(i), Operator:=xlFilterValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM