-3

私が実際にやりたいことは、ソース範囲の各列からピボットテーブルの対応する列に条件付き書式をコピーすることだけです。近づいていますが、フォーマットを適用しようとするとエラー 1004 が発生します。コードは次のとおりです。

    Sub CreatePivot()
        ' Define RngTarget and RngSource as Range type variables
        Dim RngTarget As Range
        Dim RngSource As Range
        Dim intLastCol As Integer
        Dim intCntrCol As Integer
        Dim ws As Worksheet
        Dim pt As PivotTable

        Set ws = ThisWorkbook.Sheets("Sheet2")
        ws.Cells.Clear

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

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

        ' Select the Range
        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

        ' 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

        ' ERROR! Error when "ws.Range(Cells(5, intCtrCol)).Select" is first called
        For intCntrCol = 3 To intLastCol
            ws.Range(Cells(5, intCtrCol)).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 intCntrCol
    End Sub
4

1 に答える 1

3

これは、あなたが疑ったように、最後の質問とまったく同じ問題ですが、修正が必要な問題がもう少しあります。今後、重要な Excel VBA 開発を行う場合は、.Select.

または、変更する前にピボット テーブル シートを選択することもできます。これはベスト プラクティスではありませんが、コードを実行できるようになることに注意してください。

この行を「エラー!」のすぐ上に追加します。コメント:

ws.Select

for ループの最初の行の変数名のスペルが間違っています。カウンターが呼び出されintCntrCol、それを として参照しますintCtrCol。作業中にコードをコンパイルする習慣を身につければ、この種のエラーは決して発生しません。デバッグ -> コンパイル

使用している構文では範囲を選択できません。それ以外の

ws.Range(Cells(5, intCtrCol)).Select

あなたが必要

ws.Cells(5, intCntrCol).Select
于 2013-05-02T12:48:22.293 に答える