Excel VBA 在页眉和页脚中打印日期和页码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5904967/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:10:47  来源:igfitidea点击:

Excel VBA to print the date and page number in the header and footer

excelvbaprintingpagination

提问by Tom

I would like that whenever my page prints on the top left corner for the date to be printed and the bottom left for the page number.

每当我的页面在左上角打印日期和左下角打印页码时,我都希望这样做。

How can this be done using the head command in excel.

如何使用 Excel 中的 head 命令完成此操作。

Head = "value of the date"

head = "日期值"

and

Foot = "Page number"

脚=“页码”

I know there are other ways to do this however I was asked to do this using specifically these functions.

我知道还有其他方法可以做到这一点,但是我被要求专门使用这些函数来做到这一点。

回答by Lance Roberts

With ActiveSheet.PageSetup
  .LeftHeader = Format(Now(), "dd/mm/yy")
  .LeftFooter = "&P"
End With

回答by FreeSoftwareServers

With ActiveSheet.PageSetup
  .LeftHeader = Format(Now(), "dddd, mmmm dd, yyyy")                                     ' Saturday, July 13, 2019
  .LeftFooter = "Page &P of &N"
End With

Date Formats --> https://powerspreadsheets.com/vba-format-date/#Format-Date-Using-VBA-Display-Several-Items-Of-The-Date

日期格式 --> https://powerspreadsheets.com/vba-format-date/#Format-Date-Using-VBA-Display-Several-Items-Of-The-Date

Update:

更新:

I used to use .LeftFooter = "Page " & ActiveSheet.Index & " of " & ActiveSheet.PageSetup.Pages.Count ' Page 1 of 2but this gave inconsistent results and I found it better to use .LeftFooter = "Page &P of &N"

我曾经使用过,.LeftFooter = "Page " & ActiveSheet.Index & " of " & ActiveSheet.PageSetup.Pages.Count ' Page 1 of 2但这给出了不一致的结果,我发现使用它更好.LeftFooter = "Page &P of &N"

See here for more &Fooexamples --> https://docs.microsoft.com/en-us/office/vba/excel/concepts/workbooks-and-worksheets/formatting-and-vba-codes-for-headers-and-footers

有关更多&Foo示例,请参见此处--> https://docs.microsoft.com/en-us/office/vba/excel/concepts/workbooks-and-worksheets/formatting-and-vba-codes-for-headers-and-页脚