1
根據範圍的值,我有以下代碼循環搜索範圍並格式化一些PivotItems
。代碼從一張表(帶有學生姓名和答案)跳轉到另一張表(帶有兩個RowLabels,QuestionNo和StudentName的透視表),並且當它找到正確的PivotItem時,更改其格式。在vba中選擇PivotItems的問題
Sub Macro1()
Dim student As String, q1 As String
Dim pt as PivotTable
Worksheets("SheetPivot").Activate
Set pt = ActiveSheet.PivotTables(1)
'Jumps to the Sheet with the range to loop
Worksheets("QuizState").Activate
'Goes to the first value
Range("A1").Select
'Sarts the loop
Do Until IsEmpty(ActiveCell.Value)
'Determines what type of value is.
student = ActiveCell.Value
q1 = ActiveCell.Offset(0,1).Value
If q1 = "WRONG" then
'Jumps to Sheet with PivotTable
Worksheets("SheetPivot").Activate
pt.PivotFields("StudentName").PivotItems(student).LabelRange.Select
Selection.Interior = 65535 'Yellow
Worksheets("QuizState").Activate
End If
ActiveCell.Offset(1,0).Select
Loop
End Sub
現在,由於某種原因,它的工作原理有時,有時它返回一個Run-time error '1004': Unable to get the PivotItems property of the PivotField Class
我的猜測是由於沒有找到student
項目,但它肯定是存在的。
有人可以幫我把debbug的代碼?
這確實是做什麼,我需要的。出於某種原因,我寫的代碼找到了Pivot Item,但並沒有在所有情況下選擇它(名稱已被修剪)。繞過選擇,代碼就像一個魅力。非常感謝。 – Pragabhava 2012-03-09 02:07:16