Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 34

Thread: delete

  1. #1
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location

    delete

    hi

    i need a help with a macro
    i need that if cells value in column "B" sheet1 is non numeric or letter delete the entire row

    thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try recording a macro using F5/Special to see if that produces the correct result. Your question is not very clear. Letter is by definition, non-numeric.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    hi
    sorry
    i meant that cell value is not numbers and not letters

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    This one scrubs rows with empty cells in column B too, remove
    [VBA]IsEmpty(Cells(rw, "B")) Or[/VBA]
    if you don't want this.

    [VBA]Sub blah()
    Set xxx = Intersect(ActiveSheet.UsedRange, Columns("B"))
    For rw = xxx.Row + xxx.Rows.Count - 1 To xxx.Row Step -1
    If IsEmpty(Cells(rw, "B")) Or Not IsNumeric(Cells(rw, "B")) Then ActiveSheet.Rows(rw).Delete
    Next rw
    End Sub
    [/VBA]
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try this
    [VBA]
    Option Explicit
    Sub DelChars()
    Dim rng As Range, i As Long
    Dim RegExp
    Set RegExp = CreateObject("VBScript.RegExp")
    With RegExp
    .Global = True
    .IgnoreCase = True
    End With
    RegExp.Pattern = "\w"
    Set rng = Intersect(Columns(2), ActiveSheet.UsedRange)
    For i = rng.Cells.Count To 1 Step -1
    If Not (RegExp.test(rng(i))) Then rng(i).EntireRow.Delete
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    hi
    thanks

    when i run this macro it deletes every thing even if the cells value is a number or a letter

  7. #7
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    i can not get it to work
    it does not delete stuff like "---------------------"

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by oleg_v
    hi
    thanks

    when i run this macro it deletes every thing even if the cells value is a number or a letter
    which macro?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    this one

    Quote Originally Posted by mdmackillop
    Try this
    [vba]
    Option Explicit
    Sub DelChars()
    Dim rng As Range, i As Long
    Dim RegExp
    Set RegExp = CreateObject("VBScript.RegExp")
    With RegExp
    .Global = True
    .IgnoreCase = True
    End With
    RegExp.Pattern = "\w"
    Set rng = Intersect(Columns(2), ActiveSheet.UsedRange)
    For i = rng.Cells.Count To 1 Step -1
    If Not (RegExp.test(rng(i))) Then rng(i).EntireRow.Delete
    Next
    End Sub
    [/vba]

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I suspect a regex engine flavour problem with mdmackillop's solution.
    Does mine work for you?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    hi
    it seems nothing is working
    i attached the example file
    look at the column "B" and if the cell does not contain number or letters delete
    the row.
    only for if this condition exists delete the row else not

    thanks

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Works for me on your sample. What Excel version are you using?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Olag,

    I didn't test Pascal's, but Malcom's worked for me as described (xl2003 currently, but leaving...).

    I am curious if we are understanding. In the below, should all the rows above 'DIM_1' be deleted, or just the row below 'Flatness'?

    Mark

  14. #14
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Bearing in mind clarification in msg#3, have tested md's solution and it works fine in xl2007 too. Mine will delete the wrong stuff.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  15. #15
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    hi

    thanks this is working pefectly

    can please explain to me how you macro is working line by line

    please it is very important to me


    thanks


    Quote Originally Posted by mdmackillop
    Try this
    [vba]
    Option Explicit
    Sub DelChars()
    Dim rng As Range, i As Long
    Dim RegExp
    Set RegExp = CreateObject("VBScript.RegExp")
    With RegExp
    .Global = True
    .IgnoreCase = True
    End With
    RegExp.Pattern = "\w"
    Set rng = Intersect(Columns(2), ActiveSheet.UsedRange)
    For i = rng.Cells.Count To 1 Step -1
    If Not (RegExp.test(rng(i))) Then rng(i).EntireRow.Delete
    Next
    End Sub
    [/vba]

  16. #16
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Here are some links that I have found helpful reference Regular Expressions. I personally owe a big thanks to Pedro (pgc01) at mrexcel for his long suffering patience in explaining the basics.

    http://msdn.microsoft.com/en-us/libr...1x(VS.85).aspx

    http://www.aivosto.com/vbtips/regex.html

    http://www.regular-expressions.info/tutorial.html

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    VBAX Tutor
    Joined
    Dec 2009
    Posts
    295
    Location
    i need to to some studying and fast
    myibe you can give me a link to a good book for me print out

    thanks for all you help

    i never forget

    friend


    thanks

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Google for RegExp . There is a lot out there.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  20. #20
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by mdmackillop
    My appreciation as well Malcom; thank you. Already added to favorites to reference :-)

    A great day to you and yours,

    Mark

Posting Permissions

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