【问题标题】:how to extend the following excel VBA code [duplicate]如何扩展以下excel VBA代码[重复]
【发布时间】:2017-08-19 08:20:37
【问题描述】:

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

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

“A”列——月结日

“B”栏——EMI

“C”列——兴趣部分

“D”列——主要部分

等等。

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

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

【问题讨论】:

  • 考虑到您的要求,数据透视表似乎可以解决问题。将您的数据转换为 Excel 表格,插入数据透视表,在行区域中拖动日期列并按月份对日期进行分组,然后将列拖到需要汇总的值区域中。
  • 没有数据,一旦填写了几个字段就会生成数据,一旦为一个申请人生成数据,我必须计算每列的总和并在另一张表中过去,然后我必须计算另一个申请人等等..
  • @anil,您的问题似乎是知道 A 列的最后一行?并使用最后一列对其他列 B、C、D ... 求和?
  • @anil 那又怎样?您也可以基于空 Excel 表创建数据透视表。您开始填充表格并刷新数据透视表,如果数据透视表满足您的要求,这就是您所需要的。

标签: excel vba


【解决方案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 行有公式,复制并粘贴周期数以获取日期和计算。最后,对列进行求和。我希望这能解决你的问题!

【讨论】:

  • 如何扩展它以计算多个列的总和,例如“C”、“D”和“E”
  • Sub anil111() Dim N As Long N = Cells(Rows.Count, "c").End(xlUp).Row Cells(N + 1, "c").Formula = "= SUM(c1:c" & N & ")" 结束子
  • 不工作它正在删除整个数据
【解决方案2】:

请根据我对您想要做什么的理解尝试以下编辑:

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

【讨论】:

  • 我的数据是这样的...单元格 A1 EMI 开始日期,A2 EMI 结束日期和 A3 是 EMI 的编号,例如 50 个月,B 列,C 和 D 列的数据值正在变化自动通过更改上述三个单元格值,从第 10 行开始的 B、C、D 列中的数据,即 B10:B60、C10:C60 等。使用上述代码和 A1、A2、A3 生成的列 A10:A60 月结束日期每个申请人的价值观都会改变。现在,我想在数据末尾的 B.C 和 D 的每一列中得到总和,当更改 A1、A2、A3 时,请帮助
  • 我可以知道什么是“EMI”吗?我猜你只需要改变上面 sum 函数的范围。我把它作为垂直(B1:B10)。
  • 你现在可以试试编辑后的代码,告诉我有什么问题吗?
  • EMI 相当于贷款的每月分期付款
  • sh.cells(1,1) = application.worksheetfunction.sum(ws.range("B"&1&":"&"B"&":"&iRow).value)
猜你喜欢
  • 1970-01-01
  • 2014-10-25
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多