世界热文:【实例08-间隔行插入分页符】Excel表格VBA编程实例 代码分享
Private Sub CommandButton处理_Click()
'判断工作簿名,工作表名不为空
With ThisWorkbook.Worksheets("操作界面")
(资料图片)
If Trim(.Cells(2, "C").Value) = "" Or Trim(.Cells(6, "C").Value) = "" Or Trim(.Cells(10, "C").Value) = "" _
Or Trim(.Cells(10, "D").Value) = "" Or Trim(.Cells(14, "C").Value) = "" Then
MsgBox "参数不能为空"
Exit Sub
End If
'On Error GoTo 处理出错
'定义变量
Dim wbname As String
Dim shname As String
wbname = Trim(.Cells(2, "C").Value)
shname = Trim(.Cells(6, "C").Value)
Dim splitrow As Long
Dim startnum As Long
Dim stopnum As Long
splitrow = Trim(.Cells(14, "C").Value)
startnum = Trim(.Cells(10, "C").Value)
stopnum = Trim(.Cells(10, "D").Value)
End With
'处理表格
With Workbooks(wbname).Worksheets(shname)
Dim i
For i = startnum To stopnum Step splitrow
.HPageBreaks.Add before:=.Cells(i, 1)
Next i
End With
Workbooks(wbname).Save
MsgBox "处理完成"
Workbooks(wbname).Activate
ActiveWindow.WindowState = xlMaximized
Workbooks(wbname).Worksheets(shname).Activate
Workbooks(wbname).Worksheets(shname).Cells(1, 1).Select
Exit Sub
处理出错:
MsgBox Err.Description
End Sub