+ Reply to Thread
Results 1 to 3 of 3

VBA to delete rows

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2010
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    4

    VBA to delete rows

    I am trying to write a macro that deletes rows based on certain criteria. I have rows with the word "Summary:" in it that I want to keep (no problem). My issue is that i then have rows that all begin with "Application:". What comes after that is different (i.e. Application: Start, Application: End). I need help creating this wildcard search as I have tried with no succes. This is what I have so far:

    Sub A()
    Set objExcel = CreateObject("Excel.Application")
    objExcel.Visible = True
    
    Set objWorkbook = objExcel.Workbooks.Open("C:\Documents and Settings\MANNM4\Desktop\Copy of appt center (dec 2009)-test")
    i = 1
    
    Do Until objExcel.Cells(i, 1).Value = "Done"
        If objExcel.Cells(i, 1).Value <> "Summary:" And objExcel.Cells(i, 2).Value <> "Offered" Then
            Set objRange = objExcel.Cells(i, 1).EntireRow
            objRange.Delete
            i = i - 1
        End If
        i = i + 1
    Loop
    
    End Sub
    Last edited by Leith Ross; 02-01-2010 at 02:34 PM. Reason: Corrected Code Tags

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: VBA to delete rows

    Hello mannm4,

    You can use the Like operator to perform partial matches on strings.
      'To test if the beginning of the string starts with Application: Comparisons are case sensitive.
       objExcel.Cells(i, 1).Value Like "Application:*"
    Returns True if the start of the string matches, otherwise False.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA to delete rows

    Hi,

    Wherever possible you should try and avoid looping through cells, particularly with large ranges since there is a time overhead each time the VBE jumps back to the Excel Application (and vice versa) to perform a task, as it does each tine it deletes a row. It's always much better to use standard excel functionality and use that.

    So in this case make the first part of the macro a task to create an Excel function that identifies which rows should be deleted and copy that down a helper column adjacent to your data. For instance

    =IF(AND(A1)="Summary",Left(B1,6)="Applic"),"Delete","Keep")
    Now filter your range using this helper column and select on "Delete", then finally delete all the filtered rows. You can use VBA code like:

    Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).EntireRow.Delete
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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.6.0 RC 1