我是vba的新手,我试图在两个日期之间的所有日期进入vba,例如我将使用参数01-01-2015和15-01-2015调用该函数,我将进入返回一个包含所有可能日期的数组,即:
这是我的数据;
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 :(得分: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