![]() |
|
||||||||
| Site Links |
| Consulting |
| Knowledge Base |
| Training |
| Forum |
| Articles |
| Resources |
| Products |
| Cool Tools |
| Contact |
| About Us |
| Go to Page... |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
|
Deleting Rows when using for each cell in loop
Hi All,
I have the following code that checks each cell in range for a certain condition. If it meets the criteria then the entire row is deleted. The problem I'm having is that once a row is deleted the "Next Cell" potion of the code jumps down a row rather than dealing with the next cell in the range. Is there a quick fix to amend this issue? VBA:
VBA tags courtesy of www.thecodenet.com
thanks in advance. Paddy. |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#2 |
|
Distinguished Lord of VBAX
|
bottom-up!
VBA:
VBA tags courtesy of www.thecodenet.com
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#3 |
|
|
Hi thanks will test shortly. can you tell me what the Step-1 portion of the code is doing? I've never used Step before.
|
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#4 |
|
Distinguished Lord of VBAX
|
A normal For i = ... Next loop increments i by 1 every iteration of the loop. By using Step, you can increment by some other value. In this code I am using step -1 which increments by -1, or decrements by 1, which has the effect of working from the bottom upwards.
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#5 |
|
Distinguished Lord of VBAX
|
Just spotted that I miseed a couple of references to the cell objectr
VBA:
VBA tags courtesy of www.thecodenet.com
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#6 |
|
|
Hi,
If I wanted to incorporate bank holidays into the if statement is there any easy way to do this? they are: 02/01/2012 06/04/2012 09/04/2012 07/05/2012 04/06/2012 05/06/2012 27/08/2012 25/12/2012 26/12/2012 thanks, Paddy. |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#7 |
|
Distinguished Lord of VBAX
|
Assuming the holiday dates are in M1:M10
VBA:
VBA tags courtesy of www.thecodenet.com
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#8 |
|
|
That's why MS invented autofilter: if you filter all rows that do not meet your criteria you can easily delete those.
|
|
Local Time: 04:03 AM
Local Date: 05-23-2013 |
|
|
|
#9 |
|
|
I need the dates to be built into the query something like (02/01/2012, 06/04/2012,09/04/2012). Can this be done?
|
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#10 |
|
Distinguished Lord of VBAX
|
You could stuff them into an array and use that array rather than the Range M1:M10.
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#11 |
|
|
Could you show me how to do that within the formula - I'm not familiar with arrays as they have not come up in the code I've been writing so far.
|
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#12 |
|
Distinguished Lord of VBAX
|
VBA:
VBA tags courtesy of www.thecodenet.com
____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#13 |
|
|
Hi All,
Apologies xld and sorry to butt in. I used codes from the net to solve the similar type of problem (attached is a sample file). This sample file has a "Data" & "Criteria" flap Hope this helps Paddy Kindly review. Best Regards |
|
Local Time: 08:03 PM
Local Date: 05-22-2013 Location:
|
|
|
|
#14 |
|
|
In that case I'd prefer:
VBA:
VBA tags courtesy of www.thecodenet.com
|
|
Local Time: 04:03 AM
Local Date: 05-23-2013 |
|
|
|
#15 |
|
Distinguished Lord of VBAX
|
Nor at all mate, all insights are to be welcomed.
I have just run your code and two things to note: - it only seems to delete that listed dates, whereas Paddy wants to delete all dates not in April 2012, plus weekends, plus holidays - I said seems because when I ran it it deleted nothing - this is a perennial problem using dates with autofilter. I am sure the former could be easily addressed with extra conditions. The latter works if I change the autofilter line to VBA:
VBA tags courtesy of www.thecodenet.com
As the issue is probably regional settings, I don't know if that works for you. But as it needs extra conditions, I would add a helper column with formula to check if last month, not weekends, not holidays, and filter that column by FALSE. ____________________________________________ Nihil simul inventum est et perfectum Abusus non tollit usum Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen! James Thurber |
|
Local Time: 03:03 AM
Local Date: 05-23-2013 Location:
|
|
|
|
#16 |
|
|
Hi All,
I have included the code to delete weekends. Kindly review attached file. Best Regards |
|
Local Time: 08:03 PM
Local Date: 05-22-2013 Location:
|
|
![]() |
| Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|


