Results 1 to 7 of 7

Thread: how to delete rows based on calculation

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    10

    how to delete rows based on calculation

    Hi,
    This is a VBA question - hope I have picked the right forum!
    I am not an experienced VBA person, so please be gentle!!

    I have a large excel sheet from which I need to delete certain rows,
    for instance all rows where (column K - column J < 0.6). Now this is obviously an "if" within a "for-next" but I'm not sure how to actually do the syntax in excel VBA.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: how to delete rows based on calculation

    Quote Originally Posted by spursystarman View Post
    This is a VBA question - hope I have picked the right forum!
    You actually want our Office Development forum, so I have moved this thread there.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: how to delete rows based on calculation

    how to actually do the syntax in excel VBA
    simple
    Code:
    if range("k3") <  0.6 then range("k3").entirerow.delete
    I have a large excel sheet
    as this is a large sheet, you should not delete the rows within the loop, there are 2 reasons for this, but add the ones to be deleted to a non contiguous range, then delete the range at the end
    try like
    Code:
    dim rng as range
    set rng = cells(rows.count, 1)
    For each cel in range("K:K")
        if isempty(cel) then exit for    "stop on empty cell, remove if required, but will need some alternative to avoid going to the end of sheet
        if cel < 0.6 then set rng = union(rng, cel)
    next
    rng.entirerow.delete
    this is typed directly into the browser, so may contain typos or code errors and has not been tested
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    10

    Re: how to delete rows based on calculation

    Cheers for your quick reply. I'll give that a try - so if I can use my original example this would be:
    if range("k") - range("j") < 0.6 then range("k").entirerow.delete

    I havent given a cell number, just a row, as I am expecting this to repeat inside a loop over all rows. So would I need to write something like (quick example):
    range("ki") where i is the variable for the row counter

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

    Re: how to delete rows based on calculation

    What Pete said, but with an addition: If this is a one-off, run through your sheet bottom-up.
    It's way easier since you don't have to keep an eye on the row-counter
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: how to delete rows based on calculation

    as I am expecting this to repeat inside a loop over all rows.
    i gave an example to do just that, checks every cell in column K till it comes to an empty row, then delete all rows that match the criteria
    to correct the code in the original example, change the line like
    Code:
    if cel  - cel.offset(, -1) < 0.6 then set rng = union(rng, cel)

    If this is a one-off, run through your sheet bottom-up.
    It's way easier since you don't have to keep an eye on the row-counter
    if you delete many rows, one by one the code will be very slow, that is why i gave an example to delete all rows at the end of the loop, and also avoids counter issues
    Last edited by westconn1; Aug 30th, 2021 at 05:15 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    10

    Re: how to delete rows based on calculation

    Cheers all, problem solved thanks very much.

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width