使用 PageSetup.CenterHeader 格式化文本
Formatting the texts using PageSetup.CenterHeader
我有一个表单要求 1 个字符串作为文件名,1 个长字符串作为要在 CenterHeader 中使用的数字。
我已经掌握了正确执行上述任务的能力。但是,我希望进一步 edit/format CenterHeader 中的文本(粗体、字体等...)。我似乎不知道如何正确引用它
Private Sub Export_Click()
Application.DisplayAlerts = False
Worksheets("Shelter Run").Copy
With ActiveWorkbook
Dim sht As Worksheet
Set sht = Worksheets(1)
sht.PageSetup.CenterHeader = "Shelter Run#" & TextBox1.Value
'sht.PageSetup.CenterHeader.Address.Bold = True
.SaveAs Filename:="C:\random\dir" & TextBox2.Value & ".xlsx", FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False
.Close False
End With
Application.DisplayAlerts = True
MsgBox TextBox2.Value & ".xlsx has been created!"
Unload Me
End Sub
对于 Excel,您需要使用 "escape codes" 向 HeaderFooter 添加格式。我在这个答案的末尾复制了一个 MSDN 参考,其中包含所有当前有效的代码。
为了让内容先加粗再不加粗,例如:
With ActiveWorkbook
Dim sht As Worksheet
Set sht = .Worksheets(1)
sht.PageSetup.CenterHeader = "&BShelter Run#" & TextBox1.Value & "&B Test"
.SaveAs Filename:="C:\random\dir" & TextBox2.Value & ".xlsx", _
FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False
.Close False
End With
您可能还会发现此 很有帮助,因为它包含其他类型的示例。
摘自 MSDN article:
以下特殊格式和 Visual Basic for Applications (VBA) 代码可以作为页眉和页脚属性(LeftHeader、CenterHeader、RightHeader、LeftFooter、CenterFooter 和 RightFooter)的一部分包含在内。
格式代码/说明
&L Left aligns the characters that follow.
&C Centers the characters that follow.
&R Right aligns the characters that follow.
&E Turns double-underline printing on or off.
&X Turns superscript printing on or off.
&Y Turns subscript printing on or off.
&B Turns bold printing on or off.
&I Turns italic printing on or off.
&U Turns underline printing on or off.
&S Turns strikethrough printing on or off.
&"fontname" Prints the characters that follow in the specified font. Be sure to include the double quotation marks.
&nn Prints the characters that follow in the specified font size. Use a two-digit number to specify a size in points.
&color Prints the characters in the specified color. User supplies a hexidecimal color value.
&;"+" Prints the characters that follow in the Heading font of the current theme. Be sure to include the double quotation marks.
&;"-" Prints the characters that follow in the Body font of the current theme. Be sure to include the double quotation marks.
&;K xx. Syyy Prints the characters that follow in the specified color from the current theme. xx is a two-digit number from 1 to 12 that specifies the theme color to use. *Snnn* specifies the shade (tint) of that theme color. Specify S as + to produce a lighter shade; specify S as - to produce a darker shade. nnn is a three-digit whole number that specifies a percentage from 0 to 100.
If the values that specify the theme color or shade are not within the described limits, Excel will use the nearest valid value.
VBA代码/说明
&D Prints the current date.
&T Prints the current time.
&F Prints the name of the document.
&A Prints the name of the workbook tab.
&P Prints the page number.
&P+number Prints the page number plus the specified number.
&P-number Prints the page number minus the specified number.
&& Prints a single ampersand.
&N Prints the total number of pages in the document.
&Z Prints the file path.
&G Inserts an image.
我有一个表单要求 1 个字符串作为文件名,1 个长字符串作为要在 CenterHeader 中使用的数字。
我已经掌握了正确执行上述任务的能力。但是,我希望进一步 edit/format CenterHeader 中的文本(粗体、字体等...)。我似乎不知道如何正确引用它
Private Sub Export_Click()
Application.DisplayAlerts = False
Worksheets("Shelter Run").Copy
With ActiveWorkbook
Dim sht As Worksheet
Set sht = Worksheets(1)
sht.PageSetup.CenterHeader = "Shelter Run#" & TextBox1.Value
'sht.PageSetup.CenterHeader.Address.Bold = True
.SaveAs Filename:="C:\random\dir" & TextBox2.Value & ".xlsx", FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False
.Close False
End With
Application.DisplayAlerts = True
MsgBox TextBox2.Value & ".xlsx has been created!"
Unload Me
End Sub
对于 Excel,您需要使用 "escape codes" 向 HeaderFooter 添加格式。我在这个答案的末尾复制了一个 MSDN 参考,其中包含所有当前有效的代码。
为了让内容先加粗再不加粗,例如:
With ActiveWorkbook
Dim sht As Worksheet
Set sht = .Worksheets(1)
sht.PageSetup.CenterHeader = "&BShelter Run#" & TextBox1.Value & "&B Test"
.SaveAs Filename:="C:\random\dir" & TextBox2.Value & ".xlsx", _
FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False
.Close False
End With
您可能还会发现此
摘自 MSDN article:
以下特殊格式和 Visual Basic for Applications (VBA) 代码可以作为页眉和页脚属性(LeftHeader、CenterHeader、RightHeader、LeftFooter、CenterFooter 和 RightFooter)的一部分包含在内。
格式代码/说明
&L Left aligns the characters that follow.
&C Centers the characters that follow.
&R Right aligns the characters that follow.
&E Turns double-underline printing on or off.
&X Turns superscript printing on or off.
&Y Turns subscript printing on or off.
&B Turns bold printing on or off.
&I Turns italic printing on or off.
&U Turns underline printing on or off.
&S Turns strikethrough printing on or off.
&"fontname" Prints the characters that follow in the specified font. Be sure to include the double quotation marks.
&nn Prints the characters that follow in the specified font size. Use a two-digit number to specify a size in points.
&color Prints the characters in the specified color. User supplies a hexidecimal color value.
&;"+" Prints the characters that follow in the Heading font of the current theme. Be sure to include the double quotation marks.
&;"-" Prints the characters that follow in the Body font of the current theme. Be sure to include the double quotation marks.
&;K xx. Syyy Prints the characters that follow in the specified color from the current theme. xx is a two-digit number from 1 to 12 that specifies the theme color to use. *Snnn* specifies the shade (tint) of that theme color. Specify S as + to produce a lighter shade; specify S as - to produce a darker shade. nnn is a three-digit whole number that specifies a percentage from 0 to 100.
If the values that specify the theme color or shade are not within the described limits, Excel will use the nearest valid value.
VBA代码/说明
&D Prints the current date.
&T Prints the current time.
&F Prints the name of the document.
&A Prints the name of the workbook tab.
&P Prints the page number.
&P+number Prints the page number plus the specified number.
&P-number Prints the page number minus the specified number.
&& Prints a single ampersand.
&N Prints the total number of pages in the document.
&Z Prints the file path.
&G Inserts an image.