+ Reply to Thread
Results 1 to 11 of 11

VBA to Delete nth multiple rows

Hybrid View

Ollie7957 VBA to Delete nth multiple... 03-13-2019, 11:44 AM
BellyGas Re: VBA to Delete nth... 03-13-2019, 12:38 PM
Ollie7957 Re: VBA to Delete nth... 03-13-2019, 12:42 PM
BellyGas Re: VBA to Delete nth... 03-13-2019, 12:50 PM
BellyGas Re: VBA to Delete nth... 03-13-2019, 12:49 PM
BellyGas Re: VBA to Delete nth... 03-13-2019, 12:57 PM
Ollie7957 Re: VBA to Delete nth... 03-13-2019, 01:00 PM
Winon Re: VBA to Delete nth... 03-13-2019, 01:14 PM
Sintek Re: VBA to Delete nth... 03-13-2019, 02:48 PM
Ollie7957 Re: VBA to Delete nth... 03-14-2019, 06:08 AM
Sintek Re: VBA to Delete nth... 03-14-2019, 06:15 AM
  1. #1
    Registered User
    Join Date
    09-12-2016
    Location
    England, UK
    MS-Off Ver
    MS 365
    Posts
    59

    VBA to Delete nth multiple rows

    Good afternoon all,

    I am hoping someone can help with some code I am trying to write, although I know some VBA, certain things still elude me!

    On the attached spreadsheet, I basically want to put a "Yes" or an "No" in column H, an I need a piece of code that will then go to the "Data" tab and delete all the corresponding "No" rows from the "Data" sheet, so I am left with 2 lines of data in the "Data" tab where there is a "Yes" next to the person's name.

    It sounds very basic as I am typing this, however my knowledge is only beginner, so thank you in advance for your help!
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA to Delete nth multiple rows

    The cell values shown on the control sheet are taken from the data sheet

    eg: =IF(ISBLANK(Data!B6),"",Data!B6)

    if you start deleting rows from the data sheet you'll end up with lots of #REFs on the control sheet.

  3. #3
    Registered User
    Join Date
    09-12-2016
    Location
    England, UK
    MS-Off Ver
    MS 365
    Posts
    59

    Re: VBA to Delete nth multiple rows

    The cell values shown on the control sheet are taken from the data sheet

    eg: =IF(ISBLANK(Data!B6),"",Data!B6)

    if you start deleting rows from the data sheet you'll end up with lots of #REFs on the control sheet.
    I did understand that would happen, although thank you for pointing it out. I will be writing in a part of the macro to copy the formula from somewhere else and fill down all the relevant cells, I have done this many times before and it works a treat, but that isn't the issue.

    The issue I have is how to get the VBA to delete the lines in the first place.

  4. #4
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA to Delete nth multiple rows

    You may need an "application.displayalerts = false" in there as well or you'll be clicking lots of 'are you sure' messages.

  5. #5
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA to Delete nth multiple rows

    
    sub DeleteStuff
    
    Dim Sweep as Long
    Dim DataSweep as Long
    
    
    for Sweep = 2 to range("Control!A1048000").end(xlup).row
    
         If Range("Control!H" & sweep).value = "No" then
              For DataSweep = range("Data!A1048000").end(xlup).row to 2 step -1 
                     if range("Data!A" & datasweep).value = range("Control!A" & sweep).value then                 
                             sheets("Data").rows(datasweep & ":" & datasweep).entirerow.delete
                     end if
              Next DataSweep
         End if
    Next Sweep
    
    End Sub

    Not tested, just off top of head.
    Last edited by BellyGas; 03-13-2019 at 12:59 PM.

  6. #6
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA to Delete nth multiple rows

    Edited above code. I'd completely missed the IF statement to check if the K number matches.


    And for clarity:

    range("Control!A1048000").end(xlup).row - will return the row number of the final row with any data in it in column A. ie, find the last row to search to.




    For DataSweep = range("Data!A1048000").end(xlup).row to 2 step -1 - again, finds the last row with data in it but the loop counts backwards, ie from the last row upwards to row 2. This is cos you're deleting rows so if you were counting downwards you'd miss the next row every time you deleted one.
    Last edited by BellyGas; 03-13-2019 at 12:59 PM.

  7. #7
    Registered User
    Join Date
    09-12-2016
    Location
    England, UK
    MS-Off Ver
    MS 365
    Posts
    59

    Re: VBA to Delete nth multiple rows

    Thank you very much BellyGas, I will try this out and let you know!

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA to Delete nth multiple rows

    Wrong solution, will correct and post back
    Last edited by Winon; 03-13-2019 at 05:17 PM.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  9. #9
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,233

    Re: VBA to Delete nth multiple rows

    Just for the fun of it...Taking into account the mention of you correcting the formula's referencing data sheet which will error...
    Another simple solution...
    Option Explicit
    
    Sub Ollie()
    Application.ScreenUpdating = False
    With Sheets("Data")
        .Range("E2:E" & .Range("D" & Rows.Count).End(xlUp).Row).Formula = "=INDEX('Control'!$H$2:$H$1048576,MATCH(B2,'Control'!$C$2:$C$1048576,0))"
        With .Cells(1).CurrentRegion
            .AutoFilter 5, "No"
            .Offset(1, 0).EntireRow.Delete
        End With
        '.Columns("E:E").ClearContents
        .AutoFilterMode = False
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by Sintek; 03-13-2019 at 02:55 PM.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  10. #10
    Registered User
    Join Date
    09-12-2016
    Location
    England, UK
    MS-Off Ver
    MS 365
    Posts
    59

    Re: VBA to Delete nth multiple rows

    Thank you all so much, they all worked and do what I need it to!!! Thank you for the help everyone!!

  11. #11
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    O365 V2408
    Posts
    15,233

    Re: VBA to Delete nth multiple rows

    ...........................................
    Thanks.gif

+ 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. [SOLVED] Delete Rows except rows with specific values across multiple uniquely named Worksheets
    By moosetales in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2016, 03:04 PM
  2. [SOLVED] Autofilter to delete multiple rows with multiple variables
    By Ravana in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-10-2015, 12:01 AM
  3. Replies: 9
    Last Post: 12-20-2012, 10:29 AM
  4. Excel freeze when I delete multiple rows (visible rows after filtering)
    By Snoopy2003 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-27-2012, 09:05 AM
  5. [SOLVED] How to Delete Multiple Consecutive Blank Rows - Delete all Blank Rows
    By raw_geek in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 11-16-2012, 03:17 PM
  6. Delete Multiple Rows then Delete Group
    By PY_ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2011, 10:03 AM
  7. Replies: 4
    Last Post: 06-11-2010, 03:29 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