Consulting

Results 1 to 7 of 7

Thread: Delete Row if Cell Contains More Than 10 Characters

  1. #1
    VBAX Regular
    Joined
    Nov 2011
    Posts
    23
    Location

    Delete Row if Cell Contains More Than 10 Characters

    Hello All,

    I've been searching for a code that will simply do the following.

    I have a spreadsheet that has both shipment tracking numbers and serial numbers in the same column.

    If in column "N" a cell is greater than 10 characters then delete the entire row. I want to keep all serial numbers that are 10 characters long without spaces or special characters.

    The cells in the column will contain both alpha and numeric characters.

    This code will be inserted in an existing VBA macro.

    Thank you very much.

    Ike

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    With Activesheet
    
        lastrow = .Cells(.Rows.Count, "N").End(xlUp).Row
        For i = lastrow to 2 Step -1
    
            If Len(.Cells(i, "N").Value) > 10 Then
    
                .Rows(i).Delete
           End If
        Next i
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    hey ike. you mean spaces and special characters will not be taken into account when calculating the lengt of a string? (and what special characters?)
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
        [N1:N100] = [if(N1:N100="","",if(len(N1:N100)>10,"",N1:N100))]
        Columns(14).SpecialCells(4).EntireRow.Delete
    End Sub

  5. #5
    VBAX Regular
    Joined
    Nov 2011
    Posts
    23
    Location

    Delte Row if Cell Contains More Than 10 Characters

    Hi Mancubus.

    Sometimes when data is brought over from a website a cell can expand and appear to make a row height twice the size (from 12.75 to 25.5) because for instance...

    If you type a letter, let's say "A" and then press ALT/Enter there is now an invisible special character that takes up space and when a vlookup from another source is executed on that cell the result will be #N/A. The same result occurs when there is a space either in front of the string of 10 characters or at the end.

    Thank you for asking

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. so you need to "clean" the strings first. after that you can use either procedure posted by xld and snb. ----- googling "excel vba to remove special characters from string" gives many examples.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    VBAX Regular
    Joined
    Nov 2011
    Posts
    23
    Location
    Thanks again to everyone. This is solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •