Excel VBA:在每个输出下面添加一个空白单元格

时间:2014-12-03 10:58:00

标签: excel vba excel-vba

我的VBA代码有点问题。我使用下面的代码显示日期范围内的每个日期。因此,如果日期范围是12月3日 - 12月5日,它将在单元格E10中显示3月12日,在单元格E11中显示12月12日,在单元格E12中显示5月12日。这很好,但我需要在每个日期下面添加4个空白单元格(因此12月3日将在单元格E10中,但12月4日将在单元格E15中显示等)。

Private Sub cmdOK_Click()
Dim RowCount As Long
Dim ctl As Control
' Check user input
    If Me.SdPicker.Value = "" Then
        MsgBox "Please enter an start date.", vbExclamation, "Start data error"
        Me.SdPicker.SetFocus
        Exit Sub
    End If
    If Me.EdPicker.Value = "" Then
        MsgBox "Please enter the end date.", vbExclamation, "End date error"
        Me.EdPicker.SetFocus
        Exit Sub
    End If
' Write data to worksheet
With Worksheets("Projection_Daily").Range("X1")
    .Value = Me.SdPicker.Value
End With
With Worksheets("Projection_Daily").Range("Y1")
    .Value = Me.EdPicker.Value
End With
' Close the form to open Output sheet and implement date range
        Worksheets("Projection_Daily").Activate
        Worksheets("Projection_Daily").Columns(5).ClearContents

        Dim StartDate As Date
        Dim EndDate As Date
        Dim NoDays As Integer

        StartDate = Worksheets("Projection_Daily").Range("X1").Value
        EndDate = Worksheets("Projection_Daily").Range("Y1").Value
        NoDays = EndDate - StartDate + 4

        Worksheets("Projection_Daily").Range("E10").Value = StartDate

        Worksheets("Projection_Daily").Range("E10").Resize(NoDays).DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:=xlDay, Step:=1, Stop:=EndDate, Trend:=False

        Unload Me
End Sub

这是我目前使用的代码,我试图将.Offset(4,0)插入代码但没有运气。我希望你能帮助我。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用这样的循环条件:

i = 1   'To loop through the no of days
j = 10  'To start from row no 10

For i = 1 To NoDays  'Nodays = Enddate-Startdate
     Sheet1.Cells(j, 5).value = StartDate
     StartDate = StartDate + 1
j = j + 5  'To add a gap of 4 blank rows
Next i