在此先感谢您的帮助,我似乎无法使其正常工作,而我无法弄明白。我只关心MONTH和YEAR,而不是DAY。我想拍这些数据(上校A,B,C):
Data Min Date Max Date
a 4/10/1985 10/30/2012
b 4/1/1996 11/1/2010
c 4/1/1997 11/1/2010
d 4/1/1998 11/1/2010
并自动填写G列中从最小到最大日期的每月自动填充,并在列H中填充相关数据列a到d,如下所示:
(Col.H) (Col.G)
a 4/10/1985
a 5/10/1985
a 6/10/1985
a 7/10/1985
a 8/10/1985
因此,您每月自动填充所有a,自动填充所有b,自动填充所有c,并自动填充所有d,其前面带有a,b,c,d值。
我的代码现在看起来像这样,但它只是自动填充a,而不是其他3个数据点。
Dim Summary As Worksheet
Dim DataCell As Range
Dim DataRange As Range
Dim SummaryLastRow As Range
Dim StartDate As Date
Dim EndDate As Date
Dim NoDays As Integer
Set Summary = Worksheets("Summary")
Set SummaryLastRow = Summary.Range("G" & Rows.Count).End(xlUp).Rows
Set DataRange = Summary.Range(Summary.Range("A2"), Summary.Range("A" & Rows.Count).End(xlUp).Rows)
On Error Resume Next
For Each DataCell In DataRange
StartDate = DataCell.Offset(0, 1).Value
EndDate = DataCell.Offset(0, 2).Value
NoDays = EndDate - StartDate + 1
If IsEmpty(SummaryLastRow.Offset(1, 0)) Then
SummaryLastRow.Offset(1, 0) = StartDate
SummaryLastRow.Offset(1, 0).Resize(NoDays).DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:= _
xlMonth, Step:=1, Stop:=EndDate, Trend:=False
Range(SummaryLastRow.Offset(1, 0), SummaryLastRow.Offset(1, 0).End(xlDown)).Offset(0, -1).Value = DataCell
End If
Next DataCell
任何帮助将不胜感激!谢谢!
答案 0 :(得分:0)
问题在于,只要你完成循环SummaryLastRow.Offset(1, 0)
的第一次迭代就不再是Empty
,所以其他迭代不会做任何事情。
您需要将语句设置SummaryLastRow
放在循环中,并且因为知道它下面的单元格将为空,所以If
语句变得毫无意义。
Dim Summary As Worksheet
Dim DataCell As Range
Dim DataRange As Range
Dim SummaryLastRow As Range
Dim StartDate As Date
Dim EndDate As Date
Dim NoDays As Integer
With Worksheets("Summary")
Set DataRange = .Range("A2", .Range("A" & .Rows.Count).End(xlUp))
For Each DataCell In DataRange
StartDate = DataCell.Offset(0, 1).Value
EndDate = DataCell.Offset(0, 2).Value
NoDays = EndDate - StartDate + 1
Set SummaryLastRow = .Range("G" & .Rows.Count).End(xlUp)
SummaryLastRow.Offset(1, 0) = StartDate
SummaryLastRow.Offset(1, 0).Resize(NoDays).DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:= _
xlMonth, Step:=1, Stop:=EndDate, Trend:=False
Range(SummaryLastRow.Offset(1, 0), SummaryLastRow.Offset(1, 0).End(xlDown)).Offset(0, -1).Value = DataCell.Value
Next DataCell
End With