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