如何扩展以下excel VBA代码

我想为一个给定的开始和结束周期生成月结束明智的数据; 不同申请人的月数有所不同。

以下数据将由用户input生成,并且每个申请人可能会有所不同:

列'A' – 月份结束date

“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 

@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 

点击这里下载解决scheme。

在Excel中,有5列:月底date,EMI,利息,本金,未付金额。 您必须有4个input字段:开始时间段,结束时间段,金额,兴趣。 计算button运行上面的macros。 第一行,即第九行有公式,并复制和粘贴期数以获得date和计算。 最后,列的总和被采取。 我希望这能解决你的问题!

请根据我能理解你想做什么来尝试下面的编辑:

 Sub GenerateDates() Dim startDate As Date Dim endDate As Date Dim currentDate As Date Dim ws as Worksheet Dim iRow As Long Set ws = Worksheets("Sheet1") 'or whatever name of sheet you have Set wbDesti = Workbooks.Open("C:\Users\...\DestinationFile.xlsx") ' <<< path to source workbook Set sh = wbDesti.Worksheets("Sheet1") 'Sheet in destination file 'automatically find the last row in Sheet A. iRow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _ SearchDirection:=xlPrevious, LookIn:=xlValues).row + 1 startDate = ws.cells(1,1).Value endDate = ws.cells(2,1).Value currentDate = startDate 'Range("a17").Select 'removed this to avoid .Select functions dim row as integer 'declare another variable for row... 'row = iRow 'Do Until currentDate = endDate 'ws.cells(row,1).value = currentDate 'row = row + 1 'currentDate = DateAdd("m", 1, currentDate) 'currentDate = DateSerial(Year(currentDate), Month(currentDate) + 2, 0) 'Loop Dim col as integer col = 2 'start with B Do until col = 4 sh.cells(1,col) = application.worksheetfunction.sum(ws.range("B"&10&":"&"B"&":"&60)) '***other codes goes here to transfer data same as above. col = col + 1 Loop wbDesti.quit Set sh = Nothing End Sub