Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

vba loop repeat tasks

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA loop or repeat tasks

VBA loop or repeat tasks

ResolvedVersion 2016

Quek has attended:
Excel VBA Advanced course

VBA loop or repeat tasks

Dear team,

How do i repeat this task until there is no more texts "N/A" in the column?


rrow = Sheets("Sheet 2").Range("A" & Rows.Count).End(xlUp).Row

For r = 1 To rrow
If Cells(r, "G").Text = "#N/A" Then
Sheets("Sheet 2").Rows(r).EntireRow.Delete
End If
Next

Somehow, when i run the macros once not all the rows get deleted?
And if i run it a few times then i get the data that i want.

Hope you can help.

Thanks.

RE: VBA loop or repeat tasks

Hi Shi Lin,

Thank you for the forum question.

If you start from the top you have the problem, because every time you delete a row all rows will move up and you will skip testing a row.

Try:

rrow = Sheets("Sheet 2").Range("A" & Rows.Count).End(xlUp).Row

For r = rrow To 1 step -1
If Cells(r, "G").Text = "#N/A" Then
Sheets("Sheet 2").Rows(r).EntireRow.Delete
End If
Next


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Tue 19 Mar 2019: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Transpose text

You can transpose any range of cells, turning the columns into rows and the rows into columns. Just follow these steps:

Select the range.

Click the Copy button on the Standard toolbar to copy it to the Clipboard.

Select a cell outside of the range you copied.

Select Paste Special from the Edit menu.

In the Paste Special dialog box, click Transpose, then OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.