2016-10-03 15 views
-4

私はvbaの初心者です。私はvbaに2つの日付の間のすべての日付を取得しようとしています。例えば、パラメータ01-01-2015と15-01 -2015、そして私はすべての日付の可能性のある配列を返します:vbaで2日間の日付をすべて取得する

これは私が持っているデータです。

ID  Start Date End Date Code 
1234567 03-10-2016 15-10-2016 ABC_987654321 
3456789 10-09-2016 20-09-2016 ABC_123456789 

は、結果は以下の通りであるべきであり、開始日に空白を見つけたときに停止する必要があり

アレイ

ID  Date  Code 
1234567 03-10-2016 ABC_987654321 
1234567 04-10-2016 ABC_987654321 
1234567 05-10-2016 ABC_987654321 
3456789 10-09-2016 ABC_123456789 
3456789 11-09-2016 ABC_123456789 
3456789 12-09-2016 ABC_123456789 
3456789 13-09-2016 ABC_123456789 
3456789 14-09-2016 ABC_123456789 
3456789 15-09-2016 ABC_123456789 
3456789 16-09-2016 ABC_123456789 
3456789 17-09-2016 ABC_123456789 
3456789 18-09-2016 ABC_123456789 
3456789 19-09-2016 ABC_123456789 
3456789 20-09-2016 ABC_123456789 
+0

出力には、6/10、7/10、8/10、... 15/10/2016の出力がありますか?日付を下にドラッグし、一度に1日増やすことができます。それは動作しませんか?これまでに何を試しましたか?これはかなりシンプルなことですが、オンラインでたくさんのヒントを見つけてください。何をしようとしているのか、何が働いていないのかを示してください。 – BruceWayne

+2

日付は数字です。次のループが役立ちます。研究のビットはあなたのためにそれを見つけたでしょう。グーグルで約422,000件の結果(0.80秒) –

+0

@brucewayneはo/pは現在の日付(または何か)までを意味する:)または終了日を意味します。 –

答えて

1

私は助けるために、コメントで、この問題のためにあなたのVBAコードを提供していますあなたはプロセスを理解しています。

何が起きているのかをよく理解してください。次回はこのような問題に遭遇したときに、どこから始めたらよいか理解しています。あなたが行って立ち往生した場合は、お気軽に助けを求めてください。しかし、あなたが使ったコード/数式と共に試したことについての情報を提供してください。

Sub ExampleMacro() 

' Define the variables 
Dim LastRow As Long 
Dim addrows 
Dim FindDates 
Dim CountDays 
Dim adddays 
Dim i As Long 
Dim ir As Long 

' Workout number of rows containing data to process 
With Sheets("Sheet1") 
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row - 1 
End With 

' This is the row number we want to start processing, every time the For runs, it will add another 1 to this number so the next row is processed 
addrows = 2 

' Loop through until, LastRow has been reached 
For ir = 1 To LastRow 

' Define the number of days between the two dates 
FindDates = Sheets("Sheet1").Range("B" & addrows).Value 

' Define the number of days between the two dates 
CountDays = Sheets("Sheet1").Range("C" & addrows).Value - Sheets("Sheet1").Range("B" & addrows).Value + 1 

' Define the date to enter into Data column on Sheet 2, every time it loops through the date will be increased by 1 
adddays = 0 

' Loop through until, the last date has been reached 
For i = 1 To CountDays 

' Insert a new blank row on Sheet2 - Row2, for the data to be entered 
Sheets("Sheet2").Rows("2:2").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 
' Put ID value of the row into Sheet2 
Sheets("Sheet2").Range("A2").Value = Sheets("Sheet1").Range("A" & addrows).Value 
' Put the date into Sheet2 
Sheets("Sheet2").Range("B2").Value = FindDates + adddays 
' Put the Code into Sheet2 
Sheets("Sheet2").Range("C2").Value = Sheets("Sheet1").Range("D" & addrows).Value 

' Increase the date by 1 day, ready for the re run of this loop 
adddays = adddays + 1 

' Go back to i and carry on until the number of CountDays has been matached. 
Next I 

' Now that all the dates for the first row of data has been processed, increase the row number by 1 
addrows = addrows + 1 

' Go back to ir and carry on until the number of rows with data has been completed 
Next ir 

End Sub 
関連する問題