For Next loop recursively

dinotom

Active Member
Joined
Aug 2, 2009
Messages
357
How does one run a loop backwards, ie start from the end and work up

This code which deletes Appointments from my Outlook;
Code:
For Each objAppt In objItemsInDateRange
         'Debug.Print objAppt.Body
            objAppt.Delete
Next

works perfectly fine EXCEPT it doesnt delete every item. I have to run it many times to get the number of items to 0. It needs to start at the bottom and go item by item up to the top. Should I be using Do While? ie
Do While objItemsInDateRange > 0 ?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
What is objItemsInDateRange exactly?

Is it some sort of collection?

If it is and it has a count property then you might be able to loop backwards through it, which is the usual way to hand this sort of thing.
 
Upvote 0
The solution is

Code:
 iCount = objItemsInDateRange.Count 
   'recursively loop through the items and delete all appointment items
    For i = iCount To 1 Step -1
      Set objCurItem = objItemsInDateRange.Item(i)
      If TypeName(objCurItem) = "AppointmentItem" Then
         objCurItem.delete
      End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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