2016-10-03 251 views
-4

我是vba中的新手,我想在vba中獲取所有日期之間的所有日期,例如我將使用參數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?您可以拖動日期,並將日期一次增加一天。這不行嗎?你試過什麼了?這是一件非常簡單的事情,你應該在網上找到很多提示。請顯示你的嘗試,什麼/沒有工作。 – BruceWayne

+2

日期是數字,下一個循環將有所幫助。一點研究會爲你找到它。 Google搜索它大約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 
相關問題