在多列中显示小计

时间:2012-07-19 08:27:17

标签: vba excel-vba excel

我想要做的是在每个创建的小计中,如果值为1,则为SUM。我尝试过使用Subtotal选项,但我无法使其工作(也许是因为我不擅长使用Excel),有时候我得到0和1的总和。所以我决定制作一个VBA。

这几乎就是数据组

- |    A    |    B    |  C  |
-----------------------------
1 | Names   | Points  |  N  |
-----------------------------
2 | Grimer  |   1     |  88 |
3 | Grimer  |   1     |  88 |
4 | Grimer  |   0     |  88 |
5 | Psyduck |   1     |  54 |
6 | Psyduck |   0     |  54 |
7 | Psyduck |   0     |  54 |
8 | Pikachu |   1     |  25 | 
9 | Pikachu |   1     |  25 |
10| Pikachu |   1     |  25 |

这就是我想要的:

     |    A    |    B    |  C  |
-------------------------------
   1 | Names   | Points  |  N  |
-------------------------------
|  2 | Grimer  |   1     |  88 |
|  3 | Grimer  |   1     |  88 |
|  4 | Grimer  |   0     |  88 |
-  5 | Grimer  |   2     |  88 |
|  6 | Psyduck |   1     |  54 |
|  7 | Psyduck |   0     |  54 |
|  8 | Psyduck |   0     |  54 |
-  9 | Psyduck |   1     |  54 |
|  10| Pikachu |   1     |  25 | 
|  11| Pikachu |   1     |  25 |
|  12| Pikachu |   1     |  25 |
-  13| Pikachu  |   3     |  25 |

在每行开头(从2开始)的虚线是我想要创建的组(每行的B单元格是结果..它应该是粗体)。以下代码用于获取上一个结果(表)。它的工作原理,但我想知道是否有一种方法可以在没有编程的情况下在Excel上进行更好的解决方案。或者......是否有任何错误,因为当我运行它时,需要几秒钟(超过4000行)。

Sub GetPointsByPokemon()

    With ThisWorkbook.ActiveSheet

        Dim Fila As Integer, InitRow As Integer, Suma As Integer

        PkNumber = .Range("C2").Value
        InitRow = 2
        ActualRow = 2
        Suma = 0

        Do Until .Cells(ActualRow, 1).Value = ""
            If .Cells(ActualRow, 2).Value = 1 Then
                Suma = Suma + 1
            End If

            If PkNumber <> .Range("C" & (ActualRow + 1)).Value Then
                .Cells(ActualRow, 1).Offset(1).EntireRow.Insert
                PkNumber = .Range("C" & (ActualRow + 2)).Value

                .Range(ActualRow & ":" & ActualRow).Offset(1).Value = .Range(ActualRow & ":" & ActualRow, 1).Value

                .Range("B" & (ActualRow + 1)).Value = Suma
                .Rows(InitRow & ":" & ActualRow).Group

                ActualRow = ActualRow + 1
                InitRow = ActualRow + 1
                Suma = 0
            End If

            ActualRow = ActualRow + 1
        Loop

    End With
End Sub

所有数据都是一个例子,我不得不在代码上更改一些值。我使用MySQL完成了它,但我真的想知道如何在excel(和vba)上做到这一点,提前谢谢!

2 个答案:

答案 0 :(得分:1)

就个人而言,我喜欢 Bulbasaur。

我也不喜欢这种方法,因为您正在将小计硬编码到工作表并将其编织到源数据中

但是如果您仍然想要这样做,这里是代码。它很丑,但它确实有效。

Sub AddSubtotals()

  Dim i As Long
  Dim numberOfRows As Long

  ' number of pokemon
  numberOfRows = Cells(Rows.Count, "A").End(xlUp).Row

  ' do bottom row first
  Cells(numberOfRows + 1, 1).value = Cells(numberOfRows, 1).value
  Cells(numberOfRows + 1, 2).FormulaR1C1 = "=SUMIF(R[-" & numberOfRows - 1 & "]C[-1]:R[-" & numberOfRows - (numberOfRows - 1) & "]C[-1],""" & Cells(numberOfRows, 1).value & """,R[-" & numberOfRows - 1 & "]C[0]:R[-" & numberOfRows - (numberOfRows - 1) & "]C[0])"
  ' convert to value
  Cells(numberOfRows + 1, 2).value = Cells(numberOfRows + 1, 2).value
  Cells(numberOfRows + 1, 3).value = Cells(numberOfRows, 3).value

  Range(Cells(numberOfRows + 1, 1), Cells(numberOfRows + 1, 3)).Font.Bold = True

  ' insert blank row in between each group of pokemon
  ' loop backwards because we are inserting rows
  For i = numberOfRows To 3 Step -1
    If Cells(i, 1).value <> Cells(i - 1, 1).value Then
      Cells(i, 1).EntireRow.Insert xlShiftDown

      ' copy pokemon name down
      Cells(i, 1).value = Cells(i - 1, 1).value

      ' put formula into Points field
      Cells(i, 2).FormulaR1C1 = "=SUMIF(R[-" & i - 1 & "]C[-1]:R[-" & i - (i - 1) & "]C[-1],""" & Cells(i, 1).value & """,R[-" & i - 1 & "]C[0]:R[-" & i - (i - 1) & "]C[0])"
      ' convert to value
      Cells(i, 2).value = Cells(i, 2).value

      ' copy N value down
      Cells(i, 3).value = Cells(i - 1, 3).value

      Range(Cells(i, 1), Cells(i, 3)).Font.Bold = True

    End If
  Next i

End Sub

代码前的示例数据:

before running code

代码后的示例数据:

after running code

答案 1 :(得分:1)

没有VBA的一种方式:

  1. 选择数据|小计 ... 每次更改时:Names 使用功能Sum检查PointsN

  2. 选择列C并选择将小计公式中的9,替换为(Ctrl + H)另一个摘要函数,例如:1,(平均值),4,(最大),或5,(分钟)。

  3. enter image description here

相关问题