excel - 更改 PivotField Excel VBA 上的字体样式
问题描述
我有一个构建我的数据透视表的代码,我想将其中一个字段设置为斜体,但我不知道如何。
我的代码从一开始就是这样的:
With ActiveSheet.PivotTables("Laddsida").PivotFields("Work hrs")
.Orientation = xlDataField
.Position = 6
.Function = xlSum
.NumberFormat = "[h]:mm:ss"
.Name = "Uppskattad arbetad tid"
End With
试过这个:
With ActiveSheet.PivotTables("Laddsida").PivotFields("Work hrs")
.Orientation = xlDataField
.Position = 6
.Function = xlSum
.NumberFormat = "[h]:mm:ss"
.font.Italic = True
.Name = "Uppskattad arbetad tid"
End With
但是得到以下错误:
运行时错误“438”:
对象不支持此属性或方法
在您的帮助下,我设法做到了这一点,但RowRange
唯一影响的是第一个,如图所示
解决方案
根据我的评论:.Font
不是 PivotFields 对象模型已知的属性的一部分。但是,您可以使用PivotField.DataRange
which 将返回一个Range
对象,而该对象又支持.Font
; 例如:
Sub Test()
Dim ws as Worksheet: Set ws = ThisWorkbook.Worksheets("????") 'Name of your sheet
ws.PivotTables("Laddsida").PivotFields("Work hrs").DataRange.Font.Italic = True
End Sub
根据您的评论,我认为您的行中有标题,也称为RowRange
. 我们可以使用此Range
对象来查找您的特定目标标头:
Sub Test()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("????") 'Name of your sheet
Dim cl As Range
Set cl = ws.PivotTables("Laddsida").RowRange.Find("Work hrs")
If Not cl Is Nothing Then
cl.Font.Italic = True
End If
End Sub
根据您的最新评论,您似乎有多个具有相同值的标头,因此您可能想要使用.FindNext
:
Sub Test()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("????") 'Name of your sheet
Dim cl As Range, rw As Long
With ws.PivotTables("Laddsida").RowRange
Set cl = .Find("Work hrs")
If Not cl Is Nothing Then
rw = cl.Row
Do
cl.Font.Italic = True
Set cl = .FindNext(cl)
If cl Is Nothing Then GoTo DoneFinding
Loop While cl.Row <> rw
End If
DoneFinding:
End With
End Sub
推荐阅读
- r - 制作具有 2 个因子的频率选项卡的最简单的 R 函数是什么?
- python-3.x - 如何使用 beautifulsoup 从中获取值?
- node.js - 选择所有对象的日期在 mongodb 的字段值的两个日期之间
- spring - Spring集成触发定时器事件
- powershell - PowerShell 内容替换和转义双引号
- ios - 项目无法在 XCode10.3 中构建,Apple match-o 链接器错误与 swift 库
- node.js - 有没有办法在仍然使用 mongoose 进行模式定义的同时访问 mongodb node.js 驱动程序功能?
- c++ - 视觉工作室不尊重 Windows 批处理文件中的环境变量
- python - 如何将 numpy 矩阵的 col 移动到最后一个 col?
- javascript - 以角度获取折扣百分比