gpt4 book ai didi

vba - 可旋转的困境

转载 作者:行者123 更新时间:2023-12-04 20:46:08 27 4
gpt4 key购买 nike

似乎其他人的所有示例都与我的代码中已有的示例一样。但就我而言,没有什么能解决问题。所以这里是完整的代码以及一个中断来显示我遇到问题的地方:

    Sub CreatePivot()
' Define RngTarget and RngSource as Range type variables
Dim RngTarget As Range
Dim RngSource As Range
Dim intLastCol As Integer
Dim intLCPivot As Integer
Dim intLRPivot As Integer
Dim intCntrCol As Integer
Dim intX, intY As Integer
Dim ws1, ws2 As Worksheet
Dim pt As PivotTable
Dim cf As FormatCondition

Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
ws2.Cells.Clear

' RngTarget is where the PivotTable will be created (ie: Sheet2, Cell B3)
Set RngTarget = ws2.Range("B3")

' RngSource defines the Range that will be used to create the PivotTable
Set RngSource = ws1.UsedRange

' Select the Range
ws1.Select ' Not sure why this is needed, but 1004 if omitted
RngSource.Select

' Copy the Range into the clipboard
RngSource.Copy

' Create a new PivotTable using the RngSource defined above
ActiveWorkbook.PivotCaches.Create(xlDatabase, RngSource).CreatePivotTable RngTarget, "PivotB3"
Set pt = RngTarget.PivotTable

'' This is where the the problem is. After the pivottable is created, if I look at sheet2, I see
'' only the wizard. Like this: http://wikisend.com/download/617932/ptwizard.jpg
'' I need to have all of the fields automatically selected to be included in
'' the report. I haven't been able to test anything beyond this, although it compiles without
'' a problem.

' Get the last col and row from the pivottable
intLCPivot = pt.DataBodyRange.Columns(pt.DataBodyRange.Columns.Count).Column
intLRPivot = pt.DataBodyRange.Rows(pt.DataBodyRange.Rows.Count).Row

' Get the last used column from the data table
intLastCol = RngSource.Columns(RngSource.Columns.Count).Column

' Select the Pivot table so we can apply the conditional formats
pt.PivotSelect "", xlDataAndLabel, True

'' This also causes an error. I need to be able to get the conditional format from Sheet1:B3 and apply it
'' to all Data cells in the PivotTable Report, instead of hardcoding the format like it is now.

'cf = ws1.Range("B3").FormatCondition

ws2.Select
For intX = 1 To intLCPivot
For intY = 1 To intLRPivot
ws2.Cells(4, intCntrCol).Select ' Select the current Sum column
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=5000" ' Set conditional format to less than 5000
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority ' Take priority over any other formats
With Selection.FormatConditions(1).Font ' Use the Font property for the next operations
.ThemeColor = xlThemeColorLight1 ' Set it to the default (if it does not meet the condition)
.TintAndShade = 0 ' Same as above
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535 ' Set the background color to Yellow
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'Selection.FormatConditions(1).ScopeType = xlFieldsScope ' Apply the format to all rows that match "Sum of xxxx"
Next intY
Next intX
End Sub

最佳答案

如果将其添加到脚本中代替错误行,您将获得所有名称并将其添加到枢轴:

For Each p In pt.PivotFields
Debug.Print p.Name
pt.AddDataField p,,xlSum
Next

我相信这会让你越界。 :)

关于vba - 可旋转的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com