2016-09-08 21 views
1

对于VBA,我很新。我会解释我的项目。将单元格中的日期插入到数据透视表中以将其用作过滤器

我有一个Excel工作表,有很多我想要过滤的信息。就目前我的代码看起来是这样的:

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
     "CREATION_DATE") 
     .Orientation = xlPageField 
     .Position = 1 
    End With 

ActiveSheet.PivotTables("Tableau croisé dynamique2").AddDataField ActiveSheet. _ 
    PivotTables("Tableau croisé dynamique2").PivotFields("AMOUNT"), _ 
    "Nombre de AMOUNT", xlCount 

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields("STATUS") 
    .Orientation = xlRowField 
    .Position = 1 
End With 

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
    "VENDOR_NAME") 
    .Orientation = xlRowField 
    .Position = 2 
End With 

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
    "INVOICE_NUM") 
    .Orientation = xlRowField 
    .Position = 3 
End With 

With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
    "Nombre de AMOUNT") 
    .Caption = "Somme de AMOUNT" 
    .Function = xlSum 
End With 

ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
    "CREATION_DATE").CurrentPage = "08/09/2016" 

此代码提取的日期2016年8月9日进行过滤。 现在,我想让数据透视表在启动代码之前在单元格中获取此日期。

但我不想每次都去代码来改变我想要的日期。

我想是这样的: 例如:我在单元格“A1”写“2016年9月9日”

ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields(_ 
     "CREATION_DATE").CurrentPage = "A1" 

但我得到一个调试错误。

+0

'.CurrentPage =范围( “A1”)Value2' ......你可能需要包装一个'CDATE()'周围。 –

+0

感谢您的回复,但它似乎不起作用。围绕它包装Cdate()是什么意思? (“Tableaucroisédynamique2”)。PivotFields(_ “CREATION_DATE”)。CurrentPage = Range(CDate(“'Sheet_date'!A1”))。Value2 – Thistlepower

+0

Scott说如果...... ...。 CurrentPage = Range(“A1”).Value2'不起作用,请尝试使用'... .CurrentPage = CDate(Range(“A1”).Value2)'。 (CDate将值转换为日期。)但是它实际上可能需要一个字符串,因此,如果Scott的建议都不起作用,请尝试'... .CurrentPage = Format(CDate(Range(“A1”).Value2),“dd/mm/yyyy“)'(或”mm/dd/yyyy“ - 取决于该日期的格式)。 – YowE3K

回答

0

经过一番测试,我发现原因(在我的Excel书上)。它涉及到格式化数据透视字段“CREATION_DATE”,一旦我添加了以下部分它的工作。

Set PvtFld = PvtTbl.PivotFields("CREATION_DATE") 
With PvtFld 
    .Orientation = xlPageField 
    .NumberFormat = "dd/mm/yyyy" ' << this line 
    .Position = 1 
End With 

而对于全码:

Sub PivotFilterDate() 

Dim PvtTbl    As PivotTable 
Dim PvtFld    As PivotField 

' set your Pivot Table (after created) to a variable 
Set PvtTbl = Sheets("PivotSpanish").PivotTables("Tableau croisé dynamique2") 

' Set "CREATION_DATE" to a Pivot Field variable 
Set PvtFld = PvtTbl.PivotFields("CREATION_DATE") 
With PvtFld 
    .Orientation = xlPageField 
    .NumberFormat = "dd/mm/yyyy" 
    .Position = 1 
End With 

' you can modify your code to change all field setting, like following 2: 
With PvtTbl.PivotFields("STATUS") 
    .Orientation = xlRowField 
    .Position = 1 
End With 

With PvtTbl.PivotFields("VENDOR_NAME") 
    .Orientation = xlRowField 
    .Position = 2 
End With 

' clear previous filters 
PvtFld.ClearAllFilters 

' --- trapping errors --- 
' added a Boolean Flag to trap scenarios where no Pivot Item found that matches the value in Cell A1 
Dim PvtItemExists  As Boolean 

PvtItemExists = False 

' check all Pivot items in Pivot Field ("CREATION_DATE") 
For Each PvtItem In PvtFld.PivotItems 
    ' check if current Pivot Item equals the value in Cell A1 
    If PvtItem.Value = CStr(CDate(Range("D1").Value)) Then 
     PvtItemExists = True 
     Exit For 
    End If 

Next PvtItem 

If PvtItemExists Then 
    PvtFld.CurrentPage = CStr(CDate(Range("D1").Value)) 
Else 
    MsgBox "No data matches for date " & CDate(Range("D1").Value) & " in Pivot Table", vbCritical 
End If  

End Sub 
+0

非常感谢! 我得到它的工作! – Thistlepower

+0

@Thistlepower不客气,请标为答复和upvote –

相关问题