+ Reply to Thread
Results 1 to 11 of 11

Row delete macro

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Row delete macro

    The below code is used to search the cells in column A for a certain text "String1" if the string is found the entire row is deleted. How does the code have to be if I wanted to search more than one string? I have tried this using a comma to separate the strings ex: String1,String2 but this did not work. Any help greatly appreciated .

    Sub DeleteRows()    Dim c As Range
        Dim SrchRng
         
        Set SrchRng = ActiveSheet.Range("A1", ActiveSheet.Range("A65536").End(xlUp))
        Do
            Set c = SrchRng.Find("String1", LookIn:=xlValues)
            If Not c Is Nothing Then c.EntireRow.Delete
        Loop While Not c Is Nothing
    End Sub

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Row delete macro

    One way

    Sub DeleteRows()
     Dim arrStrings, i As Long, ii As Long
     
     arrStrings = [{"string1","string2"}] 'add more values
     
     With Application
        .ScreenUpdating = False
        With ActiveSheet
            For i = .Cells(Rows.Count, "a").End(xlUp).Row To 1 Step -1
               For ii = 1 To UBound(arrStrings)
                   If .Cells(i, "a").Value = arrStrings(ii) Then
                       .Cells(i, "a").EntireRow.Delete
                    Exit For
                   End If
               Next ii
            Next i
        End With
        .ScreenUpdating = True
     End With
    End Sub
    Last edited by mike7952; 09-02-2014 at 12:05 AM.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Re: Row delete macro

    Am getting Identifier to long error. I have about 100 or less stings I'm trying to work with.

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Row delete macro

    Maybe you can list the string down column A on another sheet and this will work

    Sub DeleteRows()
     Dim arrStrings, i As Long, ii As Long
     
     'arrStrings = [{"string1","string2"}] 'add more values
     
     arrStrings = Worksheets("SomesheetName").Range("a1:a100") ' Change sheet name
     
     With Application
        .ScreenUpdating = False
        With ActiveSheet
            For i = .Cells(Rows.Count, "a").End(xlUp).Row To 1 Step -1
               For ii = 1 To UBound(arrStrings)
                   If .Cells(i, "a").Value = arrStrings(ii, 1) Then
                       .Cells(i, "a").EntireRow.Delete
                    Exit For
                   End If
               Next ii
            Next i
        End With
        .ScreenUpdating = True
     End With
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Re: Row delete macro

    So in A1 of the next sheet (sheet2) I would put the full string in this format "string1","string2" ?

    What part of the code would have to be changed if I want to search column C instead of A?

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Row delete macro

    I would put the strings in cell by themself Cell A1 = String 1, A2= string2,A3 = string3 ect...
    You can change the range below in the red
     
     arrStrings = Worksheets("SomesheetName").Range("a1:a100") ' Change sheet name

  7. #7
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Re: Row delete macro

    that will not work for me. It would have to be stings in a continuous line like the original code you gave.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Row delete macro

    Give this a try

    Sub DeleteRows()
     Const cColumnToSearch As String = "a" 'Change here for your needs
     Dim arrStrings, i As Long, ii As Long
      
     ' In cell c1 list your strings separated by a comma. No quoates Example string1,string2,string3
     arrStrings = Split(Worksheets("SomesheetName").Range("c1").Value, ",") ' Change sheet name for your needs
     
     With Application
        .ScreenUpdating = False
        With ActiveSheet
            For i = .Cells(Rows.Count, cColumnToSearch).End(xlUp).Row To 1 Step -1
               For ii = LBound(arrStrings) To UBound(arrStrings)
                   If .Cells(i, cColumnToSearch).Value = arrStrings(ii) Then
                       .Cells(i, cColumnToSearch).EntireRow.Delete
                    Exit For
                   End If
               Next ii
            Next i
        End With
        .ScreenUpdating = True
     End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Re: Row delete macro

    Okay thanks, what part of the code needs to changed to search column C instead of A of the sheet I'm running the macro on?

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551
    At the top of code change a to c
    const cColumnToSearch as string  = "a"
    Last edited by mike7952; 09-02-2014 at 02:05 AM.

  11. #11
    Forum Contributor
    Join Date
    05-07-2013
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    215

    Re: Row delete macro

    Yeap that seems to work! Thanks for the help really appreciate it!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Run Macro Automatically When delete row , changes cell,delete column
    By elevisse in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-27-2012, 07:42 AM
  2. Macro to delete certain columns and delete rows based on time in another column
    By beepbeep27 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-12-2012, 11:47 AM
  3. Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-21-2005, 08:12 PM
  4. Replies: 0
    Last Post: 06-21-2005, 01:05 PM
  5. Re: Macro to delete sheets and saves remaining file does not properly delete module
    By pherrero in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-21-2005, 01:05 PM

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