更改 PivotField 上的字体样式 Excel VBA
Change fontstyle on 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
但是得到如下错误:
Run-time error"438":
Object doesn't support this porperty or method
在你的帮助下我已经走到这一步了,但是 RowRange
只影响第一个,如图所示
enter image description here
根据我的评论:.Font
不是 PivotFields object 模型已知属性的一部分。但是,您可以使用 PivotField.DataRange
,它将 return 变成 Range
object,而后者又支持 .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
根据您的评论,我认为您的行中有 header,也称为 RowRange
。我们可以使用这个 Range
object 来找到你的特定目标 header:
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
根据您的最新评论,您似乎有多个具有相同值的 header,因此您可能想要使用 .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
我有一个构建我的数据透视表的代码,我想将其中一个字段设置为斜体,但我不知道该怎么做。
我的代码一开始是这样的:
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
但是得到如下错误:
Run-time error"438":
Object doesn't support this porperty or method
在你的帮助下我已经走到这一步了,但是 RowRange
只影响第一个,如图所示
enter image description here
根据我的评论:.Font
不是 PivotFields object 模型已知属性的一部分。但是,您可以使用 PivotField.DataRange
,它将 return 变成 Range
object,而后者又支持 .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
根据您的评论,我认为您的行中有 header,也称为 RowRange
。我们可以使用这个 Range
object 来找到你的特定目标 header:
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
根据您的最新评论,您似乎有多个具有相同值的 header,因此您可能想要使用 .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