Get the last loop in a for loop

aRush113

New Member
Joined
Nov 10, 2023
Messages
17
Office Version
  1. 2019
Platform
  1. Windows
Good day everyone.
Is there a way to find the last iteration of a for-loop?
I have a for loop that creates a list of used dates and time in a column but i need to add a different time to just the end date in the list.
1/1/20243/1/241/1/24 14:30:00start date1
3/1/246/1/242/1/24 14:30:00list of between dates created by loop
3/1/24 10:30:00end date1
3/1/24 14:30:00start date2
4/1/24 14:30:00list of between dates created by loop
5/1/24 14:30:00list of between dates created by loop
6/1/24 10:30:00end date2
The end date needs to be different than the start date of the next list or it will be counted twice and it only HAS to be a counted once.

Basically i need the last iteration number of the loop to change the time.

Thanx in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Is there a way to find the last iteration of a for-loop?

Since a FOR loop almost always has a defined number of iterations, it is fairly straightforward to find the last iteration:

VBA Code:
Sub Misc()
    For I = 1 To 100
        '
        ' Loop code goes here
        '
        If I = 100 Then
            'last loop
            MsgBox "This is the last iteration!"
            '
            'Special code goes here
            '
        End If
    Next I
End Sub
 
Upvote 0
I have a for loop that creates a list of used dates
Can you post the code for the loop please as it would help to see the syntax you are using i.e. there is a difference between
VBA Code:
Dim i as long
For i = 1 to 100
'more code
Next i

and

VBA Code:
Dim cell as Range
For Each cell in Range("B37:B234")
'more code
Next Cell
 
Upvote 0
Can you post the code for the loop please as it would help to see the syntax you are using i.e. there is a difference between
VBA Code:
Dim i as long
For i = 1 to 100
'more code
Next i

and

VBA Code:
Dim cell as Range
For Each cell in Range("B37:B234")
'more code
Next Cell
Test vba list of dates.xlsm
ABCDEFGHIJ
1Room N°CheckinN° NightsCheckoutCheckin TimeCheckout Time
2531/08/23303/09/2331/08/2023 5 14:30FALSO514:3010:30
3207/07/23411/07/2301/09/2023 5 14:30FALSO5
4107/07/23916/07/2302/09/2023 5 14:30FALSO5There is a Worksheet_Change event that runs the macro when values change in columns A to C
5514/07/23317/07/2303/09/2023 5 14:30FALSO5
6228/08/23603/09/2307/07/2023 2 14:30FALSO2
7214/07/23216/07/2308/07/2023 2 14:30FALSO2
8314/07/23115/07/2309/07/2023 2 14:30FALSO2
9509/08/23716/08/2310/07/2023 2 14:30FALSO2
10418/07/231401/08/2311/07/2023 2 14:30FALSO2
11126/05/23430/05/2307/07/2023 1 14:30VERO1Column G is needed to generate the text in column E (Date + Room N° + Time)
12504/08/23206/08/2308/07/2023 1 14:30FALSO1
13522/04/23325/04/2309/07/2023 1 14:30FALSO1
14214/08/23115/08/2310/07/2023 1 14:30FALSO1The value in E11 is the first date of the Checkin date (It needs to have the Checkin Time Which is correct) but scrolling down to F284 i have the same date and room number but it beeing the Checkout date of that booking it needs to end with the Checkout time (10:30) thus not beeing an overbooking.
15517/07/231027/07/2311/07/2023 1 14:30FALSO1
16117/07/231027/07/2312/07/2023 1 14:30FALSO1
17426/08/23702/09/2313/07/2023 1 14:30FALSO1
18216/08/23420/08/2314/07/2023 1 14:30FALSO1
19112/08/23517/08/2315/07/2023 1 14:30FALSO1
20124/08/23428/08/2316/07/2023 1 14:30FALSO1
21527/07/23501/08/2314/07/2023 5 14:30FALSO5
22516/08/23218/08/2315/07/2023 5 14:30FALSO5
23408/08/231018/08/2316/07/2023 5 14:30FALSO5
24418/06/23725/06/2317/07/2023 5 14:30VERO5
25102/08/23911/08/2328/08/2023 2 14:30VERO2By changing the time value in cell F284 to 10:30 i won't get the overbooking I need the last iteration number of each loop to change the time from 14:30 to 10:30
26202/08/231012/08/2329/08/2023 2 14:30FALSO2
27220/08/23222/08/2330/08/2023 2 14:30FALSO2
28508/07/23109/07/2331/08/2023 2 14:30FALSO2
29518/08/23725/08/2301/09/2023 2 14:30FALSO2
30120/08/23323/08/2302/09/2023 2 14:30FALSO2
31420/08/23424/08/2303/09/2023 2 14:30FALSO2Obviously this list changes everytime a booking is added or cancelled.
32208/04/23109/04/2314/07/2023 2 14:30FALSO2
33401/08/23708/08/2315/07/2023 2 14:30FALSO2
34222/08/23628/08/2316/07/2023 2 14:30FALSO2
35414/07/23115/07/2314/07/2023 3 14:30FALSO3
36118/08/23119/08/2315/07/2023 3 14:30FALSO3
37128/08/231310/09/2309/08/2023 5 14:30FALSO5
38508/05/231624/05/2310/08/2023 5 14:30FALSO5
39411/09/231223/09/2311/08/2023 5 14:30FALSO5
40309/07/23312/07/2312/08/2023 5 14:30FALSO5
41212/08/23214/08/2313/08/2023 5 14:30FALSO5
42426/05/23228/05/2314/08/2023 5 14:30FALSO5
43128/07/23401/08/2315/08/2023 5 14:30FALSO5
44219/07/23524/07/2316/08/2023 5 14:30VERO5
45229/07/23130/07/2318/07/2023 4 14:30FALSO4
46102/07/23507/07/2319/07/2023 4 14:30FALSO4
47410/07/23414/07/2320/07/2023 4 14:30FALSO4
Bookings
Cell Formulas
RangeFormula
D2:D47D2=IF(B2="","",B2+C2)
F2F2=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E2)>1,TRUE,FALSE))
F3F3=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E3)>1,TRUE,FALSE))
F4F4=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E4)>1,TRUE,FALSE))
F5F5=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E5)>1,TRUE,FALSE))
F6F6=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E6)>1,TRUE,FALSE))
F7F7=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E7)>1,TRUE,FALSE))
F8F8=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E8)>1,TRUE,FALSE))
F9F9=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E9)>1,TRUE,FALSE))
F10F10=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E10)>1,TRUE,FALSE))
F11F11=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E11)>1,TRUE,FALSE))
F12F12=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E12)>1,TRUE,FALSE))
F13F13=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E13)>1,TRUE,FALSE))
F14F14=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E14)>1,TRUE,FALSE))
F15F15=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E15)>1,TRUE,FALSE))
F16F16=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E16)>1,TRUE,FALSE))
F17F17=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E17)>1,TRUE,FALSE))
F18F18=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E18)>1,TRUE,FALSE))
F19F19=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E19)>1,TRUE,FALSE))
F20F20=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E20)>1,TRUE,FALSE))
F21F21=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E21)>1,TRUE,FALSE))
F22F22=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E22)>1,TRUE,FALSE))
F23F23=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E23)>1,TRUE,FALSE))
F24F24=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E24)>1,TRUE,FALSE))
F25F25=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E25)>1,TRUE,FALSE))
F26F26=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E26)>1,TRUE,FALSE))
F27F27=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E27)>1,TRUE,FALSE))
F28F28=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E28)>1,TRUE,FALSE))
F29F29=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E29)>1,TRUE,FALSE))
F30F30=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E30)>1,TRUE,FALSE))
F31F31=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E31)>1,TRUE,FALSE))
F32F32=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E32)>1,TRUE,FALSE))
F33F33=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E33)>1,TRUE,FALSE))
F34F34=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E34)>1,TRUE,FALSE))
F35F35=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E35)>1,TRUE,FALSE))
F36F36=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E36)>1,TRUE,FALSE))
F37F37=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E37)>1,TRUE,FALSE))
F38F38=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E38)>1,TRUE,FALSE))
F39F39=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E39)>1,TRUE,FALSE))
F40F40=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E40)>1,TRUE,FALSE))
F41F41=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E41)>1,TRUE,FALSE))
F42F42=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E42)>1,TRUE,FALSE))
F43F43=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E43)>1,TRUE,FALSE))
F44F44=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E44)>1,TRUE,FALSE))
F45F45=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E45)>1,TRUE,FALSE))
F46F46=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E46)>1,TRUE,FALSE))
F47F47=IF(E2="",FALSE,IF(COUNTIF($E:$E,"="&E47)>1,TRUE,FALSE))
Named Ranges
NameRefers ToCells
OBCHECK=Bookings!$E:$EF2:F47
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B:DCell Value=$E$11textNO
E:EExpression=SE(E1="";FALSO;SE(CONTA.SE(E:E;"="&E1)>1;VERO;FALSO))textNO
F:FCell Value=VEROtextNO


VBA Code:
Sub CalcOff()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
Application.DisplayStatusBar = False
Application.CommandBars("ply").Enabled = False
End With
End Sub
Sub CalcOn()
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.EnableEvents = True
Application.DisplayStatusBar = True
Application.CommandBars("ply").Enabled = True
End With
End Sub
Sub DateRange()

Dim ws As Worksheet
Dim startdate As Date
Dim enddate As Date
Dim ckInTime As Double
Dim ckOutTime As Date
Dim outputcell As Range
Dim RMoutputcell As Range
Dim Cell As Range
Dim x As Integer
Dim lRow As Integer
Dim outRow As Integer
Dim obRow As Integer

Set ws = Sheet1
CalcOff
Range("RMCHECK").ClearContents ' Room check
Range("OBCHECK").ClearContents ' Used dates
Range("TFCHECK").ClearContents ' Overbooked check

 lRow = Range("B2").End(xlDown).Row ' Last row Checkin column
 
 ckInTime = Range("ORACI")
' ckOutTime = Range("ORACO")
    
    For actrow = 2 To lRow
        If outRow = nil Then outRow = 2 Else outRow = Range("E2").End(xlDown).Row + 1
         startdate = Cells(actrow, 2)
         enddate = Cells(actrow, 4)
    
    Set outputcell = Cells(outRow, 5)
    Set RMoutputcell = Cells(outRow, 7)

         For x = 0 To enddate - startdate
             RMoutputcell.Offset(x) = Cells(actrow, 1)
             outputcell.Offset(x) = startdate + x & " " & RMoutputcell & " " & Format(ckInTime, "h:m")
         Next x
    
    Next actrow

 obRow = Range("E2").End(xlDown).Row
 
For k = 2 To obRow 'Place Overbooking Formula in column F
    ws.Range("F" & k).Formula = "=IF(E2="""",FALSE,IF(COUNTIF($E:$E" & ",""=""&E" & k & ")>1,TRUE,FALSE))"
'    ws.Range("F" & k).Formula = "=IF(INDIRECT(ADDRESS(ROW(),COLUMN()-1,4))=INDIRECT(ADDRESS(ROW()-1,COLUMN()-1,4)),FALSE,COUNTIF($E$1:E" & k & ",""=""&E" & k & ")>1)"
Next k

    For j = 2 To k - 1
        vChk = Cells(j, 6)
        If vChk = "Falso" Then
            'do nothing
        Else
            'Show Overbooking message
            MsgBox "Overbooking"
            Exit Sub
        End If
        
    Next j
CalcOn
End Sub
 
Upvote 0
Found it, i'm using the column C value to establish the last iteration number for the loop and added
VBA Code:
Dim itCount As Integer
    itCount = Cells(actrow, 3)
and changed the for loop to
VBA Code:
         For x = 0 To enddate - startdate
             RMoutputcell.Offset(x) = Cells(actrow, 1)
             If x = itCount Then
             outputcell.Offset(x) = startdate + x & " " & RMoutputcell & " " & Format(ckOutTime, "h:m")
             Else
             outputcell.Offset(x) = startdate + x & " " & RMoutputcell & " " & Format(ckInTime, "h:m")
             End If
         Next x

Thanx to everyone for the help.
Peace ✌️
 
Upvote 0
Solution

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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