|
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
If CommandButton1.Caption = "小计合计" Then
CommandButton1.Caption = "清除"
'先排序
i = Range("b65536").End(xlUp).Row
ActiveWorkbook.Worksheets("原表").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("原表").Sort.SortFields.Add Key:=Range("A2:A" & i), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("原表").Sort.SortFields.Add Key:=Range("B2:B" & i), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("原表").Sort
.SetRange Range("A1:G" & i)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'开始计算
Columns("A:A").Insert Shift:=xlToRight
Range("A1") = "月份"
For i = 2 To 57
Cells(i, 1) = Format(Cells(i, 2), "yyyy-mm")
Next i
Range("A1:H" & Range("A65320").End(xlUp).Row).Select
Range("A1").CurrentRegion.Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(4, 5, 6, _
7, 8), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
For i = 2 To Range("A65320").End(xlUp).Row
If InStr(Cells(i, 1), "汇总") Then
Cells(i, 3) = "合计"
Cells(i, 3).Font.Bold = True
End If
Next i
Range("A1").CurrentRegion.Subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(4, 5, 6, 7, _
8), Replace:=False, PageBreaks:=False, SummaryBelowData:=True
For i = 2 To Range("A65320").End(xlUp).Row
If InStr(Cells(i, 3), "汇总") Then
Cells(i, 3) = "小计"
Cells(i, 3).Font.Bold = True
End If
Next i
Cells(Range("A65320").End(xlUp).Row, 3) = Cells(Range("A65320").End(xlUp).Row, 1)
Cells(Range("A65320").End(xlUp).Row, 3).Font.Bold = True
Columns("A:A").Delete Shift:=xlToLeft
Else
Range("A1").CurrentRegion.RemoveSubtotal
CommandButton1.Caption = "小计合计"
End If
Range("A1").CurrentRegion.Borders.LineStyle = 1
Range("A1").Select
Application.ScreenUpdating = True
End Sub
|
|