+ Reply to Thread
Results 1 to 9 of 9

Thread: VBA Command to delete the rows.

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    56

    VBA Command to delete the rows.

    Hello,

    I have created the below macro code to delete all the rows containing "account" in column B.

    Can you please help me using the below VBA codes, using IF...THEN condition.

    Range("B1").Select
    Selection.AutoFilter Field:=1, Criteria1:="=*account*", Operator:=xlAnd
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Last edited by mohan_rajun; 10-25-2010 at 06:44 AM.

  2. #2
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: VBA Command to delete the rows.

    This is how I would write that:
    Dim LR as Long
    
    Range("B1").AutoFilter
    Range("B1").AutoFilter Field:=1, Criteria1:="=*account*"
    LR = Range("B" & Rows.Count).End(xlUp).Row
    
    If LR > 1 Then Range("B2:B" & LR).Delete xlShiftUp
    ActiveSheet.AutoFilterMode = False
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  3. #3
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,294

    Re: VBA Command to delete the rows.

    Try this
    Sub DeleteAccountRows()
    Dim RowCtr As Double
    Dim LastRow As Double
    
    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For RowCtr = 1 To LastRow
        If InStr(Cells(RowCtr, "B"), "account") > 0 Then
           Rows(RowCtr & ":" & RowCtr).Delete Shift:=xlUp
        End If
    Next RowCtr
    End Sub

  4. #4
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,294

    Re: VBA Command to delete the rows.

    Because you are shifting deleted rows up it might be better to count backwards instead of forwards. Change the For line to:

    For RowCtr = LastRow to 1 Step -1

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA Command to delete the rows.

    I would advise you read through: http://www.xtremevbtalk.com/showpost...70&postcount=4

    ie if you intend to use AutoFilter then utilise SpecialCells etc...

    edit: full tutorial link: http://www.xtremevbtalk.com/showthread.php?t=300757
    Last edited by DonkeyOte; 10-25-2010 at 03:06 AM.

  6. #6
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,294

    Re: VBA Command to delete the rows.

    I think the Jerry's code will work quicker but may have a problem if there are blank rows.

    Excel people always assume users put data in well defined tables or lists.

  7. #7
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,294

    Re: VBA Command to delete the rows.

    Thanks for the link. I study now.

  8. #8
    Registered User
    Join Date
    09-23-2010
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    56

    Re: VBA Command to delete the rows.

    Hi JBeaucaire,

    Thanks for the help... It worked out....

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,224

    Re: VBA Command to delete the rows.

    Quote Originally Posted by mohan_rajun View Post
    Hi JBeaucaire,

    Thanks for the help... It worked out....
    My pleasure, glad it works for you.


    Quote Originally Posted by MarvinP
    I think the Jerry's code will work quicker but may have a problem if there are blank rows. Excel people always assume users put data in well defined tables or lists.
    I don't assume it, but I am prepared to counsel OPs to correct their data layout into standardized tables/lists when the "blanks in my design" does popup, part of what we have to do, eh?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0