|
请教老师:这是一个23行自动分页小计累计的代码,假若要改为25行和30行自动分页小计,要改那个值?谢谢!
Sub a增加小计()
Do
bt = Application.InputBox(prompt:="请输入要求和的列,用数字表示。" & Chr(13) & "如D列则输入4,多列用半角“,”分开:", Title:="输入需要求和的列")
If bt = False Then Exit Sub
bt = bt & ","
For k = 1 To Len(bt) - 1
b = Mid(bt, k, WorksheetFunction.Find(",", bt, k) - k)
If b > 1 And b < 257 Then
Else
MsgBox "输入列数格式错误,请重新输入!", vbOKOnly, "错误"
Exit For
End If
k = k + Len(b)
Next k
Loop Until k > Len(bt)
k = 0
Do
k = k + 1
If k > 50 Then MsgBox "未找到起始行。(A列序号为1的那行)", vbOKOnly, "失败": Exit Sub
Loop Until Cells(k, 1) = 1
Application.ScreenUpdating = False
For j = 4 To Range("A65536").End(xlUp).Row
If Cells(j, 1) = "本页小计" Or Cells(j, 1) = "累计" Or Cells(j, 1) = "总计" Then
Rows(j).Delete shift:=xlUp
j = j - 1
End If
Next j
Rows(j - 2 & ":" & j - 1).Copy Destination:=Range("A" & j)
Range("A" & j & ":O" & j + 1).ClearContents
Cells(j, 1) = "本页小计"
Cells(j + 1, 1) = "总计"
Rows(j & ":" & j + 1).Font.Bold = True
ActiveWindow.View = xlPageBreakPreview
For r = 1 To 2
For i = 1 To ActiveSheet.HPageBreaks.Count
Set hb = ActiveSheet.HPageBreaks(i).Location
If hb.Offset(-2, 0) <> "本页小计" Then
hb.Offset(-2, 0).EntireRow.Insert
hb.Offset(-2, 0).EntireRow.Insert
hb.Offset(-4, 0) = "本页小计"
hb.Offset(-3, 0) = "累计"
Rows(hb.Offset(-4, 0).Row & ":" & hb.Offset(-3, 0).Row).Font.Bold = True
For m = 1 To Len(bt) - 1
b = Mid(bt, m, WorksheetFunction.Find(",", bt, m) - m)
If i = 1 Then sh = "A" & k Else: sh = ActiveSheet.HPageBreaks(i - 1).Location.Address(0, 0)
hb.Offset(-4, b - 1) = "=SUBTOTAL(9," & Range(sh).Offset(0, b - 1).Address(0, 0) & ":" & hb.Offset(-5, b - 1).Address(0, 0) & ")"
hb.Offset(-3, b - 1) = "=SUBTOTAL(9," & Range("A" & k).Offset(0, b - 1).Address(0, 0) & ":" & hb.Offset(-5, b - 1).Address(0, 0) & ")"
m = m + Len(b)
Next m
End If
Next i
Next r
For m = 1 To Len(bt) - 1
b = Mid(bt, m, WorksheetFunction.Find(",", bt, m) - m)
If i = 1 Then sh = "A" & k Else: sh = ActiveSheet.HPageBreaks(i - 1).Location.Address(0, 0)
zc = Range("A65536").End(xlUp).Row
Cells(zc, 1).Offset(-1, b - 1) = "=SUBTOTAL(9," & Range(sh).Offset(0, b - 1).Address(0, 0) & ":" & Cells(zc, 1).Offset(-2, b - 1).Address(0, 0) & ")"
Cells(zc, 1).Offset(0, b - 1) = "=SUBTOTAL(9," & Range("A" & k).Offset(0, b - 1).Address(0, 0) & ":" & Cells(zc, 1).Offset(-2, b - 1).Address(0, 0) & ")"
m = m + Len(b)
Next m
ActiveWindow.View = xlNormalView
Application.ScreenUpdating = True
End Sub
Sub a删除小计()
Application.ScreenUpdating = False
For i = 4 To Range("A65536").End(xlUp).Row
If Cells(i, 1) = "本页小计" Or Cells(i, 1) = "累计" Or Cells(i, 1) = "总计" Then
Rows(i).Delete shift:=xlUp
i = i - 1
End If
Next i
Application.ScreenUpdating = True
End Sub
自动分页小计累计.rar
(21.39 KB, 下载次数: 19)
|
|