我有一个表格要求1个字符串用于文件名,1个长表示要在CenterHeader中使用的数字。
我已经让它正确执行上述任务。但是,我希望进一步编辑/格式化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
答案 0 :(得分:1)
对于Excel,您需要使用"转义码"添加格式到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
您可能还会发现此other SO Q&A很有用,因为它包含其他类型的示例。
摘自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.