Leonid询问是否可以为页面的页眉或页脚设置背景颜色。简单的答案是Excel中没有这种功能。但是,有两种方法可以解决此问题。例如,如果您使用的是Excel 2002或Excel 2003,则可以将图形添加到页眉或页脚。使用正确的图形,实际上是包含颜色的图形时,可以使页眉和页脚看上去包含颜色。

另一种选择是制作“假”的页眉和页脚。如果您要做的只是使用不同的颜色标题,则可以使用工作表的前几行作为标题。您可以根据需要设置这些行的格式,包括设置行的颜色。然后,您可以指示Excel在打印输出的每一页顶部重复这些行(为此使用“页面设置”对话框)。

在页脚区域重复行变得更加麻烦,因为Excel不包含允许您在每页底部重复行的功能。可以创建一个宏来为页眉和页脚添加行,但这确实会导致您的工作表发生更改-需要为假的页眉和页脚添加行。

例如,请考虑以下宏。假定您要在打印输出的左右两边留一英寸的边框,并且每页只希望打印46行。它设置边距,然后逐步浏览工作表,并根据需要添加伪造的页眉和页脚行。 (由于宏会调整工作表的设计,因此请确保在运行宏之前保存工作表。)

Sub FakeHeaderFooter()

Dim LHeader As String     Dim CHeader As String     Dim LFooter As String     Dim CFooter As String     Dim CBottom As Integer     Dim CRow As Integer     Dim PageSize As Integer          LHeader = "Top Left"

CHeader = "Top Center"

LFooter = "Bottom Left"

CFooter = "Bottom Center"

PageSize = 46

With ActiveSheet.PageSetup         .PrintTitleRows = ""

.PrintTitleColumns = ""

.PrintArea = ""

.LeftHeader = ""

.CenterHeader = ""

.RightHeader = ""

.LeftFooter = ""

.CenterFooter = ""

.RightFooter = ""

.LeftMargin = Application.InchesToPoints(1)

.RightMargin = Application.InchesToPoints(1)

.TopMargin = Application.InchesToPoints(0)

.BottomMargin = Application.InchesToPoints(0)

.HeaderMargin = Application.InchesToPoints(0)

.FooterMargin = Application.InchesToPoints(0)

.PrintHeadings = False         .Orientation = xlPortrait     End With

CBottom = Range("A16000").End(xlUp).Row

CRow = 1     Do Until CRow > CBottom         If CRow Mod PageSize = 1 Then             Rows(CRow).Select             Selection.Insert Shift:=xlDown             Selection.Insert Shift:=xlDown             CBottom = CBottom + 2

Cells(CRow, 1).Value = LHeader             Cells(CRow, 4).Value = CHeader             Range(Cells(CRow, 1), _               Cells(CRow, 8)).Interior.ColorIndex = 34             Range(Cells(CRow + 1, 1), _               Cells(CRow + 1, 8)).Interior.ColorIndex = xlNone             CRow = CRow + 2         ElseIf CRow Mod PageSize = PageSize - 1 Then             Rows(CRow).Select             Selection.Insert Shift:=xlDown             Selection.Insert Shift:=xlDown             CBottom = CBottom + 2

Cells(CRow + 1, 1).Value = LFooter             Cells(CRow + 1, 4).Value = CFooter             Range(Cells(CRow + 1, 1), _               Cells(CRow + 1, 8)).Interior.ColorIndex = 34             CRow = CRow + 2         Else             CRow = CRow + 1         End If     Loop

LastPageNumber = PageNumber + 1     LastRow = LastPageNumber * PageSize     If CBottom <> LastRow Then             Range(Cells(LastRow, 1), _               Cells(LastRow, 8)).Interior.ColorIndex = 34             Cells(LastRow, 1).Value = LFooter             Cells(LastRow, 4).Value = CFooter     End If

CBottom = Range("A16000").End(xlUp).Row

CRow = 2     Do Until CRow > CBottom         If CRow Mod PageSize = 1 Then             Cells(CRow, 1).PageBreak = xlManual         End If         CRow = CRow + 1     Loop End Sub

要更改每页的行数,只需更改分配给PageSize变量的值。您还可以通过更改分配给LHeader,CHeader,LFooter和CFooter变量的内容来更改“页眉”和“页脚”区域中显示的内容。

注意:

如果您想知道如何使用此页面(或_ExcelTips_网站上的任何其他页面)中描述的宏,我准备了一个特殊页面,其中包含有用的信息。

_ExcelTips_是您进行经济高效的Microsoft Excel培训的来源。

本技巧(3056)适用于Microsoft Excel 97、2000、2002和2003。