我想将命名为“ NorthHead”的N2:P3命名范围设置为中心标题。但是,使用我的代码,我在标头中仅获得单元格N2。可能是什么问题?
Sub SetCenterHeader()
ActiveSheet.PageSetup.CenterHeader = Range("NorthHead")
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End Sub
答案 0 :(得分:1)
我想您在所有这些单元格中都有一些文本,然后只需加入即可:
Option Explicit
Sub SetCenterHeader()
Dim txt As String
Dim myRow As Range
With Range("NorthHead") ' reference named range
For Each myRow In .Rows ' loop through referenced range rows
txt = txt & Join(Application.Transpose(Application.Transpose(myRow.Value)), " ") & vbLf ' update 'txt' with current row cells values joined and separated by a blank
Next
End With
ActiveSheet.PageSetup.CenterHeader = Left(txt, Len(txt) - 1) ' set CenterHeader with resulting 'txt' excluding last vblf character
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End Sub