简体   繁体   中英

adding loop values to next loop

Is it possible to keep the value of a loop and add it to the next round of the loop? I'm trying to find the difference between two columns of dates, and add the difference together (to find the average number of days later). My code so far is:

Sub macro1()
Dim d1
Dim d2
Dim i As Integer
Set wf = Application.WorksheetFunction

For i = 1 To 10
d1 = Cells(i, 1)
d2 = Cells(i, 2)
sdays = wf.NetworkDays(d1, d2)
Range("D4") = (the sum of the loops)

Next i

End Sub

try this the variable SumOfDates keeps a track of the sum of your dates as it loops and then pastes that value into your range D4

Sub macro1()
Dim d1
Dim d2
Dim i As Integer
Dim SumOfDates 
Set wf = Application.WorksheetFunction

SumOfDates =0

For i = 1 To 10
d1 = Cells(i, 1)
d2 = Cells(i, 2)
sdays = wf.NetworkDays(d1, d2)

SumOfDates = SumOfDates + sdays

Next i

Range("D4").Value = SumOfDates

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM