-
Aug 28th, 2021, 11:15 AM
#1
Thread Starter
New Member
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.
-
Aug 28th, 2021, 11:43 AM
#2
Re: how to delete rows based on calculation
Originally Posted by spursystarman
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.
-
Aug 28th, 2021, 09:47 PM
#3
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
-
Aug 29th, 2021, 10:28 AM
#4
Thread Starter
New Member
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
-
Aug 30th, 2021, 01:08 AM
#5
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
-
Aug 30th, 2021, 05:09 AM
#6
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
-
Sep 2nd, 2021, 03:44 PM
#7
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|