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

do until loops

ResolvedVersion 2007

Louisa has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Do until loops

I have a loop to update the dates on a spreadsheet. I am doing this using filtering.

The problem is that not all sheets will have info on them everyday. So there will be errors.

I need to do it so that if there is nothing on the filter then the loop gets restarted but not sure how to do it. I have written the if statement in the code but not sure how I finish. Basically

intNoOfRows = rng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1

If intNoOfRows = Then

I want to move onto the next row and start the loop again - is it possible. If its greater than zero then I need to carry on with the loop and do the other bits.


Here is the code I have for the loop/

Do Until ActiveCell = ""

strSheet = ActiveCell

Set wsFiltering = Worksheets(strSheet)

intLastRow = wsFiltering.Cells(Rows.Count, "b").End(xlUp).Row

Set rngFilter = wsFiltering.Range("a1:a" & intLastRow)

With rngFilter

.AutoFilter Field:=1, Criteria1:="="

End With

Set rng = Worksheets(strSheet).AutoFilter.Range

intNoOfRows = rng.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1

If intNoOfRows > 0 Then


With rngFilter

Set rngDates = .Resize(.Rows.Count - 1, 1).Offset(1, 0).SpecialCells(xlCellTypeVisible)

End With



With rngDates
.Value = dbleDate
.NumberFormat = "dd/mm/yyyy"

If wsFiltering.FilterMode Then
wsFiltering.ShowAllData
End If

ActiveCell.Offset(1, 0).Select

End With



Loop

RE: Do until loops

Hi Louisa

Thanks for getting in touch. I'm having trouble visualizing your data without a sample, but have you thought about a Do While?

e.g. Do While intNoOfRows <> ""

Or put the loop break condition at the end of the loop, so everything is checked once, so...

Do

<code>

Loop While intNoOfRows > 0

Let me know if this prompts any ideas and I'll see what else I can suggest.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best 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

RE: Do until loops

Sorry I have just got back from holiday.

So would a do while loop only do it if it meets a certain condition else it will start the loop again?

Or am I getting completely confused (quite possible).

Wed 6 Nov 2013: 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:

Get back to active cell

If you have scrolled away from the active celland want to get back t it quicly you can Ctrl and Backspace to get back

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.12 secs.