Help with Looping (For and Next Statement)

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help me with understanding how to use the For and Next Statement?

I have had a go at trying to delete rows one at a time with the For and Next statement but am struggling to understand how it works?

I have put (dummy Data) in columns "A:B" and have put X's down column C in random rows. I want to delete (xlUp) rows, Range("A:C") one at a time where there is an X in column C.

I have the following macros, the first two work but I have to click the play button in the Visual Basic for the rows to delete one at a time.

The third macro is my attempt at using the For and Next statement using the first macro and want to try and do the same with the second macro if possible? It deletes the first row but then errors?

any help would be appreciated


ABCDE
1HeaderHeaderHeader

2A1


3B2x

4C3


5A4x

6A5x

7B6x

8C7


9B8x

10A9


11B10x

12C11


13A12x

14




15

<tbody>
</tbody>
Code:
Sub Macro1()

Dim Sht1 As Worksheet: Set Sht1 = Sheet1
Dim lRow As Long

    lRow = Sht1.Cells(, 3).End(xlDown).Row
    
    Sht1.Range("A" & lRow & ":C" & lRow).Delete (xlUp)
    
End Sub


Sub Macro2()

Dim Sht1 As Worksheet: Set Sht1 = Sheet1

    Sht1.Cells(, 3).End(xlDown).Activate
    Sht1.Range(ActiveCell.Offset(, -2), ActiveCell.Offset(0, 0)).Delete (xlUp)

End Sub


Sub Macro3()

Dim Sht1 As Worksheet: Set Sht1 = Sheet1
Dim lRow As Long
Dim i As Long
Dim Rng

    lRow = Sht1.Cells(, 3).End(xlDown).Row
    
    Set Rng = Sht1.Range("A" & lRow & ":C" & lRow)
    
    For i = 1 To lRow
        Rng.Delete (xlUp)
    Next i

End Sub

Regards

pwill
 
Last edited:
Now I'm going to have a go at adding the final part to the code for column D.

All it does is a count, so I will add it to the end off your code and let you know how I get on.
Good to try it yourself - best way to learn - but if you run into difficulty, or speed issues again, show us what you have tried and some sample data with the required results entered manually together with explanation.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Thanks for your generous comments. Glad it worked so well for you.


I guess the exercise has something of a lesson if you have further questions in the future. Best to tell/show us as clearly as you can what you have, what you want to end up with and what you have tried so far. However, best not to tell us how the problem should be solved as there may be ways that you haven't even thought of or know about. :)

I agree, I need to tidy up my questions, I think I'm better than I was when I joined :)

I will work on that... I just panic sometimes and start rushing things

pwill
 
Upvote 0
Now I'm going to have a go at adding the final part to the code for column D.

All it does is a count, so I will add it to the end off your code and let you know how I get on.
pwill

Hi Pete,

Just letting you know I added the final part to the code and it runs perfect :) it took around 1sec to complete with the additional count to column D, which is fast enough for me :)

Thanks again

pwill
 
Upvote 0
Hi Pete,

Just letting you know I added the final part to the code and it runs perfect :) it took around 1sec to complete with the additional count to column D, which is fast enough for me :)
Great news! Thanks for letting us know. :)
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,901
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top