我需要在两张“开始”表中格式化表格,使它们看起来像“结果”表。
我的示例文件,包括三张纸:“start_1”、“start_2”和“result”。
“start_1”和“start_2”是相同的。
http://www.mediafire.com/download/n8dwli8v55lw9gl/add_a_blank_column_before_each_table.xls
我在顶部添加一个空白行并冻结标题行。
以下是任务:
- 在每个表格之前添加一个空白列。添加的列必须没有填充颜色和边框。(这里的每个表格都由顶部标题行中的合并单元格标识)
- 确定有数据的最后一列和有数据的最后一行,以便隐藏空白的外部空间。
ThisWorkbook 模块中的代码:
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim bScrUpdate As Boolean
Dim ws As Worksheet
Dim rng As Range
Application.EnableCancelKey = xlDisabled 'disable ESC key
bScrUpdate = Application.ScreenUpdating
If bScrUpdate = True Then Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "result" Then
ws.Select
Rows("1:1").Insert Shift:=xlDown
Range("A4").Select
ActiveWindow.FreezePanes = True
For Each rng In Rows("2:2").Cells
If rng.MergeCells Then
rng.MergeArea.Cells(1, 1).Select
Selection.Offset(-1, 1).EntireColumn.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End If
Next rng
End If
Next ws
Application.DisplayAlerts = True
If Not Application.ScreenUpdating = bScrUpdate _
Then Application.ScreenUpdating = bScrUpdate
Application.EnableCancelKey = xlInterrupt 'enable ESC key
End Sub