如何扩展以下excel VBA代码

时间:2017-08-19 08:20:38

标签: excel vba

我想为给定的开始和结束时段生成月末数据;不同申请人的月数不同。

以下数据将由用户输入生成,并且每个申请人的数量可能不同:

  

列'A' - 月结束日期

     

列'B' - EMI

     

列'C' - 兴趣部分

     

列'D' - 原则部分

     
    

等等。

  

我还想计算最后每列的总和以及根据列“A”no行更改工作表大小的行。请帮忙。

Sub GenerateDates()
    Dim startDate As Date
    Dim endDate As Date
    Dim currentDate As Date

    startDate = Range("b4").Value
    endDate = Range("b8").Value

    currentDate = startDate
    Range("a17").Select

    Do Until currentDate = endDate

        ActiveCell.Value = currentDate
        ActiveCell.Offset(1, 0).Select

        'currentDate = DateAdd("m", 1, currentDate)
        currentDate = DateSerial(Year(currentDate), Month(currentDate) + 2, 0)

    Loop

End Sub

2 个答案:

答案 0 :(得分:1)

请根据我对您要做的事情的理解,尝试以下编辑:

['foo','foo',1,2,3,'bar',bar','bar']

答案 1 :(得分:1)

@Anil试试这个:

Sub GenerateDates()
    Dim startDate As Date
    Dim endDate As Date
    Dim currentDate As Date
    Dim cnt As Integer

    startDate = Range("b2").Value
    endDate = Range("b3").Value

    currentDate = startDate
    Range("a9").Select

    cnt = ActiveSheet.Range("E3")
    ActiveSheet.Range(ActiveSheet.Range("A10"), ActiveSheet.Cells(Rows.Count, Columns.Count)).ClearContents

    With ActiveSheet.Range(ActiveSheet.Range("A9"), ActiveSheet.Range("A9").End(xlToRight))
        .Copy
        .Offset(1).Resize(cnt - 1).PasteSpecial xlPasteAll
    End With

    Application.CutCopyMode = False

    For i = 1 To Range("A8").End(xlToRight).Column - Range("A8").Column
        Range("A8").Offset(cnt + 1, i) = WorksheetFunction.Sum(Range("A8").Offset(1, i).Resize(cnt))
    Next
End Sub

点击here 下载解决方案。

在excel中,有5列:月末日期,EMI,利息,本金,未偿还金额。您必须有4个输入字段:开始期间,结束期间,金额,兴趣。 “计算”按钮运行上面的宏。第一行,即第9行具有公式,并且复制和粘贴用于获取日期和计算的周期数。最后,采用列的总和。我希望这能解决你的问题!