excel - Excel VBA将命名范围设置为标题
问题描述
我想将命名范围为 N2:P3 命名为“NorthHead”作为中心标题。但是,使用我的代码,我只在标题中得到单元格 N2。可能是什么问题?
Sub SetCenterHeader()
ActiveSheet.PageSetup.CenterHeader = Range("NorthHead")
ActiveWindow.SelectedSheets.PrintOut Copies:=1
End Sub
解决方案
我猜你在所有这些单元格中都有一些文本,然后加入它们:
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
推荐阅读
- html - 当侧边栏中的文本过多时屏幕会破裂
- php - 如何显示无头 chrome 实例的控制台日志和错误?
- scala - 在 Apache Ignite 中,有没有办法让 @QuerySqlField 处理可选类型?
- python - 类上的函数调用返回对象引用而不是期望值
- python - “没有名为 'django_heroku' 的模块”应用程序无法在 Heroku 上启动
- r - nls 无法拟合指数曲线来完美数据
- python - 将 on_release 操作添加到循环中的按钮
- reactjs - TypeError:this.props.chooseVideo 不是函数
- java - 如何让一个订阅者在 Radisson(redis-java 客户端)中处理消息(考虑有多个)
- excel - 有没有办法在 excel vba 2010 中连接文件路径?