vba实现分类汇总功能

Option Explicit
Sub test()
    Dim sht As Worksheet, rng As Range
    Set sht = ActiveSheet
    Dim i As Integer, index As Integer, subtotal As Double, dm As Variant
    Dim oldIndex As Integer, oldSubtotal As Double
    
    Dim startLine As Integer
    Dim lastLine As Integer
    
    startLine = 2
    lastLine = ActiveCell.CurrentRegion.Rows.Count
    
    dm = sht.Cells(lastLine, 2).Value
    subtotal = sht.Cells(lastLine, 3).Value
    index = lastLine
    For i = lastLine - 1 To startLine Step -1
        If dm <> sht.Cells(i, 2).Value Then
            dm = sht.Cells(i, 2).Value

            oldIndex = index
            oldSubtotal = subtotal

            index = i
            subtotal = sht.Cells(index, 3).Value

            sht.Cells(oldIndex + 1, 2).EntireRow.Insert
            sht.Cells(oldIndex + 1, 2).Value = "小计"
            sht.Cells(oldIndex + 1, 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值