|
|
发表于 2023-9-21 19:08:35
|
显示全部楼层
本帖最后由 teardrops 于 2023-9-21 20:33 编辑
本以为可以sum 一拉到底
还是vba吧...
抛砖引玉
用法
1.创建一个xlsm (可以运行宏)
2.alt+f11 插入模块
3.ctrl+v
4.在合并的单元格里面填写 =MJJ_SUM(b100)
- Function MJJ_SUM(ByVal sum_cell As Range, Optional ByVal mjj_cell As Range, Optional ByVal is_by_col As Integer = 1)
- Dim thisCell As Range, new_area As Range, first_cell As Range, cells_count As Integer
- Dim sr As Long, sc As Long, fr As Long, fc As Long
-
- If Not mjj_cell Is Nothing Then
- Set thisCell = mjj_cell
- Else
- Set thisCell = Application.thisCell
- End If
-
- Set first_cell = thisCell.MergeArea.Cells(1, 1)
- sr = sum_cell.Row
- sc = sum_cell.Column
- fr = first_cell.Row
- fc = first_cell.Column
-
- If thisCell.MergeCells Then
- If is_by_col = 0 Then
- cells_count = thisCell.MergeArea.Columns.Count - 1
- Set new_area = Range(Cells(sr, fc), Cells(sr, fc + cells_count))
- Else
- cells_count = thisCell.MergeArea.Rows.Count - 1
- Set new_area = Range(Cells(fr, sc), Cells(fr + cells_count, sc))
- End If
- MJJ_SUM = Application.WorksheetFunction.Sum(new_area)
- Else
- MJJ_SUM = sum_cell
- End If
- End Function
复制代码 |
|