Excel VBA - 将范围指定为页眉
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15867191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA - Specify Range as Page Header
提问by Tejas
I want to specify a Range as Page Header from my VBA Code.
我想从我的 VBA 代码中指定一个范围作为页眉。
But when I did
但是当我这样做的时候
ActiveSheet.PageSetup.LeftHeader = HeaderSheet.Range("A1:L2").Value
I takes only first cell i.e. A1 from HeaderSheet as Page Header and ignores the rest. Also it doesn't apply border in Page Header.
我只取第一个单元格,即 HeaderSheet 中的 A1 作为页眉,并忽略其余部分。它也不会在页眉中应用边框。
Any workaround for this please?
请问有什么解决方法吗?
回答by Lorin S.
So the "Left Header" would be the left-most header for three headers that describe the sheet or report. Normally it would have values such as the report title, author, date of report, etc.
因此,“左页眉”将是描述工作表或报表的三个页眉的最左侧页眉。通常它会有诸如报告标题、作者、报告日期等值。
Are you sure you don't mean to indicate the row or column headers?
您确定不是要指明行或列标题吗?
Below, the PrintTitleRows specifies rows 1 & 2 to be used as rows to repeat at the top. Alternatively columns to repeat on the left side is just the "A" column.
下面,PrintTitleRows 指定第 1 行和第 2 行用作要在顶部重复的行。或者,在左侧重复的列只是“A”列。
Sub example()
'ActiveSheet.PageSetup.LeftHeader = HeaderSheet.Range("A1:L2").Value
With ActiveSheet.PageSetup
.PrintTitleRows = ":"
.PrintTitleColumns = "$A:$A"
End With
End Sub
If you did actually intend to put the contents of cells A1-L2 into the left-most header section then you'll have to do a concatenation of the cells, I believe... viz,
如果您确实打算将单元格 A1-L2 的内容放入最左侧的标题部分,那么您必须将单元格串联起来,我相信...即,
With ActiveSheet.PageSetup
.LeftHeader = Range("a1").Value & " " & Range("b1").Value & " " & Range("a2").Value & " " & Range("b2").Value
End With