【问题标题】:VBA Macro, Formatting Excel Sheet to Sum by Index and AccountVBA 宏,格式化 Excel 表以按索引和帐户求和
【发布时间】:2016-10-14 18:05:50
【问题描述】:

我还有更多格式问题

我正在查看具有索引#(C 列)和帐户#(E 列)的交易表。我需要汇总整个索引中的所有条目,然后才能汇总帐户中的所有条目。我有一些我正在使用的代码,但我不知道如何在计算索引总数后计算帐户条目的总数。

Sub TotalIndex()
'
Dim iRow As Integer, iCol As Integer
Dim oRng As Range
Dim fRow As Integer
Dim lRow As Integer

Set oRng = Range("c4")

iRow = oRng.Row
iCol = oRng.Column
fRow = iRow
Do
'
If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
    Cells(iRow + 1, iCol).EntireRow.Insert shift:=xlDown
    iRow = iRow + 1
    Range("j" & iRow).Font.Bold = True
    Range("j" & iRow).Value = "Total"
    Range("k" & iRow).Font.Bold = True
    lRow = iRow - 1
    Range("k" & iRow).Value = "=sum(k" & CStr(fRow) & ":k" & CStr(lRow) & ")"
    iRow = iRow + 1
    fRow = iRow
Else
    iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, iCol).Text = ""
'
End Sub

Sub TotalAccount()
'
Dim iRow As Integer, iCol As Integer
Dim oRng As Range
Dim fRow As Integer
Dim lRow As Integer

Set oRng = Range("e4")

iRow = oRng.Row
iCol = oRng.Column
fRow = iRow


Dim laRow As Long
Dim laCol As Long

    laRow = Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

Do
'
    If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
        Cells(iRow + 1, iCol).EntireRow.Insert shift:=xlDown
        iRow = iRow + 1
        Range("j" & iRow).Font.Bold = True
        Range("j" & iRow).Value = "Total"
        Range("k" & iRow).Font.Bold = True
        lRow = iRow - 1
        Range("k" & iRow).Value = "=sum(k" & CStr(fRow) & ":k" & CStr(lRow) & ")"
        iRow = iRow + 1
        fRow = iRow
    Else
        iRow = iRow + 1
    End If

'
Loop While Not Cells(iRow, iCol).Text = ""
'
End Sub

我将代码分成两个函数。一个汇总索引,然后另一个功能是“尝试”汇总每个索引内的帐户。

有什么建议吗?

【问题讨论】:

  • 如您所见,一旦我点击下一个索引,它就会退出。看到我知道问题出在哪里......但我正在努力弄清楚我将如何继续前进到下一个索引......在图片中是生物学。我知道当代码遇到一个没有任何东西的单元格时,它会停止。
  • 为什么要使用宏来执行此操作? Excel 内置了分组/大纲功能,您可以使用Subtotal() 函数来做您需要的事情。

标签: vba excel macros


【解决方案1】:

看看这个教程,它会按照 Trashman 的建议做你想做的事。 https://www.youtube.com/watch?v=QuGIlPopVms

【讨论】:

  • 这应该是评论,而不是答案
猜你喜欢
  • 1970-01-01
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多