Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default How can I make For-Next loop with date?

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.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop Until LDate Column Date Tony Excel Discussion (Misc queries) 0 May 7th 09 05:49 PM
I want to make one loop David Excel Programming 2 October 12th 05 01:35 AM
How to Make a Loop count by 1% not 1 MichaelC Excel Programming 8 June 12th 05 02:04 AM
Date selection loop Roger[_8_] Excel Programming 12 September 26th 04 12:53 AM
Help with macro to make it loop through coloums R Krishna Excel Programming 0 July 29th 03 04:54 PM


All times are GMT +1. The time now is 11:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"