簡體   English   中英

復制/粘貼單元格到過濾器

[英]Copy/Paste Cell to Filter

我目前遇到了 VBA 代碼的問題,我無法使其工作。 我的想法是有 2 張紙,第一張紙只有一個單元格(單元格 C7),通過“單擊此處”按鈕輸入客戶代碼。 第二張表(名稱:Volume)是與客戶代碼(Volume、Net sale、Patterns 等)相關的所有數據的 Pivot。 我希望有一個規則,可以將可以輸入的數字(客戶代碼)復制到過濾器 pivot 字段區域(單元格 A1)的第二張表中,它會自動過濾並顯示該客戶的必要數據代碼。 我嘗試進行宏錄制,但最終只修復了一個客戶代碼(見下文):

Range("C7"). Select
Activesheet.pivotTables("Volume").PivotFields("Customer Code").ClearAllFilters
Activesheet.pivotTables("Volume").PivotFields("Customer Code").CurrentPage ="0001101073"
ActiveCell.Offset(0,1).Range("A1").Select
End Sub

如您所見,客戶代碼的數量是固定的,所以如果我輸入另一個不同的數字,它仍會自動過濾宏記錄的數字。

然后我嘗試通過將所有內容放在同一張表中來更改它,因此客戶代碼單元格中的類型是 E3 並且 pivot 字段過濾器單元格是 C3 使用此規則,但它仍然不起作用(見下文) :

Sub Filter_Click()
    If Range("E3").Value = "" Then
        MsgBox ("You must enter the customer code.")
        Exit Sub
    End If
    
    With ActiveSheet.PivotTables("Volume").PivotFields("customer code").ClearAllFilters
         ActiveSheet.PivotTables("Volume").PivotFields("customer code").PivotFilters , Add:=xlValueEquals, Value1:=Range("C3").Value
    End With  
End Sub

我很感激這種幫助。 非常感謝。問候。

你的兩次嘗試都快到了。 將它們結合起來會讓你更接近。 但是,您使用的是 C3 的值,而不是 E3 (我認為代碼應該在哪里)

嘗試以下操作:

With ActiveSheet.PivotTables("Volume").PivotFields("customer code")
    .ClearAllFilters
    .CurrentPage = Range("e3").Value
End With

更新:

如果您想在選擇無效客戶代碼的情況下進行錯誤陷阱 - 使用此:

Sub Filter_Click()
    If Range("E3").Value = "" Then
        MsgBox ("You must enter the customer code.")
        Exit Sub
    End If
    On Error Resume Next
    With ActiveSheet.PivotTables("Volume").PivotFields("customer code")
        .ClearAllFilters
        .CurrentPage = Range("E3").Value
    End With
    If Err = 1004 Then
        MsgBox "Customer code not valid"
    End If
    On Error GoTo 0
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM