1

I want to be able to filter column A. (with vba for delete what I don’t need)

Example:

  • df!gf:mqichgfdcg
  • test)2:@1jhbh5@j0
  • est@56:)hquct36A
  • h@hy.ju:A3)nxhd123QW
  • tempghj#b:jkb
  • temp234!A:gfgcjhgcj,hgk
  • hgdfht:2345vk!
  • hgchghc:268678954
  • hgchghc:A268678954

I desire filter with these specification:

  1. The filter should start after character : (for each cell)

  2. There must be at least 10 characters (uppercase, lowercase, numbers, special characters)

https://i.stack.imgur.com/v7ooi.jpg this cells (A:3, A:5, A:7, A:8) don't respects the criterias

  1. Erase lines that do not respect the criterias.

So i desire delete this cells. https://i.stack.imgur.com/kR40B.jpg

i desire delete each empy cells or lines https://i.stack.imgur.com/aj9qr.jpg

I have this code for delete each empy line

Source : Excel VBA - Delete empty rows Option Explicit

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
Pablo
  • 23
  • 3
  • 1
    Welcome to Stack Overflow. Please note that because this is no free code writing service it is necessary that you show either what you have tried so far and where you got stuck or errors (by showing your code) or at least to show what you have researched and the effort you made. Otherwise it is just asking us to do all the work for you. Reading [ask] might help you to improve your question. – Pᴇʜ May 15 '20 at 06:20

2 Answers2

1

You may be overthinking this.  I’m going to ignore the VBA part of the question for a moment.  You can filter the strings using Excel’s builtin filtering capability:

  1. Make sure that the strings are in a column with a header (e.g., set A1 to “Strings”) and filter the column.
  2. Click on the drop-down arrow for the filter, → “Text Filters” → “Contains…”.

        illustration of Filter GUI

  3. Enter :?????????? into the Contains filter.  That will match any value that contains a : followed by ten more characters.

0

You can use something like this code:

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
Sharif Lotfi
  • 544
  • 6
  • 13
  • Note that if you have a forward loop (top to bottom) and you delete rows the loop mis-counts and jumps rows. Deleting or adding rows needs the loop always to be backwards! Also you loop through *all* rows of the entire sheet `Cells.Rows.Count` which makes it extremely slow. Highly recommend to limit that to the actual data. – Pᴇʜ May 15 '20 at 06:14
  • Also `val` is a very bad variable name as there exists the [Val function](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/val-function) in VBA so `Val` is a reserved word that you should not use for variable names. – Pᴇʜ May 15 '20 at 06:19
  • yes,you're right @Pᴇʜ , I only make a point to help and lead him to solve his answer, not directly do homework, there are many ideas to answer to questions, may be better you write the exactly right answer instead of click on negative unusefull sign :\ – Sharif Lotfi May 15 '20 at 12:54
  • I down-voted because the code does not work because your counter jumps over some rows without testing them. That means the answer is not useful (that is what the button is for). And I didn't write an answer because the OP did not show any attempt to solve the issue on his own. • Also note that I remove down-votes if people fix their answers. – Pᴇʜ May 15 '20 at 13:02
  • 1
    Ok, I edited the code based on your correct comment. – Sharif Lotfi May 15 '20 at 13:11
  • I understand. However, I looked for a way to filter from the characters : but I didn’t find. thank you for your help I will keep looking. I try change Rows(i).Delete Shift:=xlUp BY cells.(i).ClearContents To delete with the other code. – Pablo May 16 '20 at 08:13