具有特殊功能的过滤单元

问题描述 投票:0回答:1

我希望能够过滤A列(用vba删除不需要的内容)。

例子。

  • df!gf:mqichgfdcg
  • 测试)2:@1jhbh5@j0
  • est@56:)hquct36A
  • [email protected]:A3)nxhd123QW
  • tempghj#b:jkb
  • temp234!A:gfgcjhgcj,hgk。
  • hgdfht:2345vk!
  • hgchghc:268678954
  • hgchghc:A268678954

我希望过滤器有以下规格。

  1. 过滤器应该从字符之后开始 : (对每个电池)

  2. 必须至少有10个字符(大写、小写、数字、特殊字符)。

https:/i.imgur.com3XiECI7.jpg。该单元(A:3, A:5, A:7, A:8)不符合标准。

  1. 删除不符合标准的行。

所以我希望删除这些单元格。https:/i.imgur.com12N5k2O.jpg

我想删除每个单元格或行https:/i.imgur.comO3nIzDt.jpg

我有这样的代码来删除每一行empy。

来源于: Excel VBA - 删除空行 选项明确

Sub Sample()
    Dim i As Long
    Dim DelRange As Range

    On Error GoTo Whoa

    Application.ScreenUpdating = False

    For i = 1 To 1000000
        If Application.WorksheetFunction.CountA(Range("A" & i & ":" & "B" & i)) = 0 Then
            If DelRange Is Nothing Then
                Set DelRange = Range("A" & i & ":" & "B" & i)
            Else
                Set DelRange = Union(DelRange, Range("A" & i & ":" & "B" & i))
            End If
        End If
    Next i

    If Not DelRange Is Nothing Then DelRange.Delete shift:=xlUp
LetsContinue:
    Application.ScreenUpdating = True

    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub
excel vba excel-vba excel-formula macros
1个回答
1
投票

你可能想多了。 我暂时不考虑问题中的VBA部分。 你可以使用Excel的内置过滤功能来过滤字符串。

  1. 确保字符串在一列有标题的列中(例如,设置... A1 到 "字符串"),然后过滤该列。
  2. 点击过滤器的下拉箭头,→"文本过滤器"→"包含..."。

        illustration of Filter GUI

  3. 输入 :?????????? 到 Contains 过滤器中。 这将匹配任何包含 :后面再跟十个字符。


0
投票

你可以使用类似这样的代码。

Sub test1()
    Dim OriginText, filterVal, startPosition
    Dim ThereIs10Char As Boolean
    Application.ScreenUpdating = False

    For i = 1 To Cells.Rows.Count ' this will be slow ,you better use integer number ( rows count number) instead of Cells.Rows.Count
        OriginText= Cells(i, "A").Value
        startPosition = InStr(1, OriginText, ":")
        filterVal = Mid(OriginText, startPosition + 1, Len(OriginText) - startPosition)
        ThereIs10Char = False
        If Len(filterVal >= 10) Then
            ThereIs10Char = True
        End If

        'I dont understand your mean for empty lines
        'you can use If condition for [while cells(i,"A").value="" Goto next i] or anything else

        If ThereIs10Char = True Then
            Rows(i).Delete Shift:=xlUp
            i = i - 1
        End If
    Next

    Application.ScreenUpdating = True
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.