ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How can I make For-Next loop with date? (https://www.excelbanter.com/excel-programming/342796-how-can-i-make-next-loop-date.html)

GreenInIowa

How can I make For-Next loop with date?
 
Hi,

I am trying to make a loop, starting date 7/1/2001 and ending date
8/12/2005, which would write the results starting with Cells(1,1). By the
way, I would like to increment it with a day was wondering how I can do this?

Thanks.

Leith Ross[_58_]

How can I make For-Next loop with date?
 

Hello GreenInIowa,

This example will list the dates in column A of the worksheet. I wasn't
sure about the incrementing by day part. Did you mean by day of the week
or day of the month?


Sub LoopDate()

Dim I
Dim R As Long

For I = CVDate("07/01/2001") To CVDate("8/12/2005")
R = R + 1
Cells(R, 1).Value = I
Next I

End Sub


This should help get you going.

Sincerely,


--
Leith Ross


------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=476116


GreenInIowa

How can I make For-Next loop with date?
 
Hi JS2004R6,

This works great for incrementing days, which I asked in my original
question. I was wondering how we modify it to increment by "month" such as
7/2/2001, 8/2/2001, ... 12/2/2001,..

Thanks.

"JS2004R6" wrote:

Hi GreenInIowa,

You can add the code below to a module in your workbook. The code will loop
through all of the dates between (and including) 7/1/2001 and 8/12/2005, and
add each date to Sheeet1 Column A, starting in Cell A1 and moving down. Then
a message box will display "Done" when it's finished.

Hope that helps.

Regards,
James

Sub DateLoop()
' DECLARATIONS
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim datStartDate As Date
Dim datEndDate As Date
Dim datCurrent As Date

' INITIALIZE
On Error GoTo ErrHandler
Set wkb = ThisWorkbook
Set wks = wkb.Worksheets("Sheet1")
Set rng = wks.Range("A1")
datStartDate = #7/1/2001#
datEndDate = #8/12/2005#

' MAIN BODY
For datCurrent = datStartDate To datEndDate
rng.Value = datCurrent
Set rng = rng.Offset(1, 0)
Next datCurrent

MsgBox "DONE"

CleanUp:
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Exit Sub

WrapUp:
GoSub CleanUp
Return

ErrHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & vbCrLf &
Err.Description, _
vbOKOnly + vbInformation, "ConditionalShading()"
GoSub WrapUp
End Sub

"GreenInIowa" wrote:

Hi,

I am trying to make a loop, starting date 7/1/2001 and ending date
8/12/2005, which would write the results starting with Cells(1,1). By the
way, I would like to increment it with a day was wondering how I can do this?

Thanks.


GreenInIowa

How can I make For-Next loop with date?
 
Hi Leith,

This is works great. But, I tried your loop without CVDate( ) for example
#07/01/2001#. It gave me similar results and was wondering why we use CVDate(
) for? Also, if we want to increment your code by "month" rather than "day",
how do we do it, for example, 4/4/2000, 5/4/2000, ....12/4/2004?

Thanks

"Leith Ross" wrote:


Hello GreenInIowa,

This example will list the dates in column A of the worksheet. I wasn't
sure about the incrementing by day part. Did you mean by day of the week
or day of the month?


Sub LoopDate()

Dim I
Dim R As Long

For I = CVDate("07/01/2001") To CVDate("8/12/2005")
R = R + 1
Cells(R, 1).Value = I
Next I

End Sub


This should help get you going.

Sincerely,


--
Leith Ross


------------------------------------------------------------------------
Leith Ross's Profile: http://www.excelforum.com/member.php...o&userid=18465
View this thread: http://www.excelforum.com/showthread...hreadid=476116



Dave Peterson

How can I make For-Next loop with date?
 
I'd use something like:

dim myStartDate as date
dim mCtr as long

mystartdate = dateserial(2001,1,1)

for mctr = 0 to 11 'whatever you want
msgbox dateserial(year(mystartdate),month(mystartdate)+mc tr,2)
next mctr


GreenInIowa wrote:

Hi JS2004R6,

This works great for incrementing days, which I asked in my original
question. I was wondering how we modify it to increment by "month" such as
7/2/2001, 8/2/2001, ... 12/2/2001,..

Thanks.

"JS2004R6" wrote:

Hi GreenInIowa,

You can add the code below to a module in your workbook. The code will loop
through all of the dates between (and including) 7/1/2001 and 8/12/2005, and
add each date to Sheeet1 Column A, starting in Cell A1 and moving down. Then
a message box will display "Done" when it's finished.

Hope that helps.

Regards,
James

Sub DateLoop()
' DECLARATIONS
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim datStartDate As Date
Dim datEndDate As Date
Dim datCurrent As Date

' INITIALIZE
On Error GoTo ErrHandler
Set wkb = ThisWorkbook
Set wks = wkb.Worksheets("Sheet1")
Set rng = wks.Range("A1")
datStartDate = #7/1/2001#
datEndDate = #8/12/2005#

' MAIN BODY
For datCurrent = datStartDate To datEndDate
rng.Value = datCurrent
Set rng = rng.Offset(1, 0)
Next datCurrent

MsgBox "DONE"

CleanUp:
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Exit Sub

WrapUp:
GoSub CleanUp
Return

ErrHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & vbCrLf &
Err.Description, _
vbOKOnly + vbInformation, "ConditionalShading()"
GoSub WrapUp
End Sub

"GreenInIowa" wrote:

Hi,

I am trying to make a loop, starting date 7/1/2001 and ending date
8/12/2005, which would write the results starting with Cells(1,1). By the
way, I would like to increment it with a day was wondering how I can do this?

Thanks.


--

Dave Peterson

GreenInIowa

How can I make For-Next loop with date?
 
Thanks, Dave.



"Dave Peterson" wrote:

I'd use something like:

dim myStartDate as date
dim mCtr as long

mystartdate = dateserial(2001,1,1)

for mctr = 0 to 11 'whatever you want
msgbox dateserial(year(mystartdate),month(mystartdate)+mc tr,2)
next mctr


GreenInIowa wrote:

Hi JS2004R6,

This works great for incrementing days, which I asked in my original
question. I was wondering how we modify it to increment by "month" such as
7/2/2001, 8/2/2001, ... 12/2/2001,..

Thanks.

"JS2004R6" wrote:

Hi GreenInIowa,

You can add the code below to a module in your workbook. The code will loop
through all of the dates between (and including) 7/1/2001 and 8/12/2005, and
add each date to Sheeet1 Column A, starting in Cell A1 and moving down. Then
a message box will display "Done" when it's finished.

Hope that helps.

Regards,
James

Sub DateLoop()
' DECLARATIONS
Dim wkb As Workbook
Dim wks As Worksheet
Dim rng As Range
Dim datStartDate As Date
Dim datEndDate As Date
Dim datCurrent As Date

' INITIALIZE
On Error GoTo ErrHandler
Set wkb = ThisWorkbook
Set wks = wkb.Worksheets("Sheet1")
Set rng = wks.Range("A1")
datStartDate = #7/1/2001#
datEndDate = #8/12/2005#

' MAIN BODY
For datCurrent = datStartDate To datEndDate
rng.Value = datCurrent
Set rng = rng.Offset(1, 0)
Next datCurrent

MsgBox "DONE"

CleanUp:
Set wkb = Nothing
Set wks = Nothing
Set rng = Nothing
Exit Sub

WrapUp:
GoSub CleanUp
Return

ErrHandler:
MsgBox "Error Number: " & Err.Number & vbCrLf & vbCrLf &
Err.Description, _
vbOKOnly + vbInformation, "ConditionalShading()"
GoSub WrapUp
End Sub

"GreenInIowa" wrote:

Hi,

I am trying to make a loop, starting date 7/1/2001 and ending date
8/12/2005, which would write the results starting with Cells(1,1). By the
way, I would like to increment it with a day was wondering how I can do this?

Thanks.


--

Dave Peterson



All times are GMT +1. The time now is 10:36 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com