Вопрос

I've a problem with my code. I'm trying to activate a code that take a cell in one sheet and filter the data in another pivot sheets, in case that the value dosen't exist there's a msgbox that show there's an error. My problem is when the value is true I'd like it to show msgbox "the value dosen't exists in the pivot". when the "if" is false I need to filter the data but it dosen't work. There's the code:

Sub MM()
    Sheets("sheets1").Select
    Selection.Copy
    Sheets("pivot").Select
    Range("C1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
    ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
    If Not IsError(ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value) Then
        MsgBox ("the value dosen't exists in the pivot")
         Sheets("sheets1").Select
    Else
        ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
    End If
End Sub

I'll be glad for some help!

Это было полезно?

Решение 2

I found the solution for my problem.

Sub MM()
Sheets("Sheets1").Select
Selection.Copy
Sheets("Pivot").Select
Range("C1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").ClearAllFilters
ActiveSheet.PivotTables("pivottable1").PivotCache.Refresh
On Error GoTo msg
ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").CurrentPage = Range("c1").Value
Exit Sub

msg:
MsgBox ("There is no data for this WBS in pivot")
Sheets("sheets1").Select
End Sub

Другие советы

Not totally sure if you wanted to filter the pivot according to what is in the selected cell, but here is my suggestion. To point out there is a way to filter pivot with many values but I supposed you wanted the filter to be done only for one value? Also the way to add filter to pivot is to loop trough all the field values and set them to visible or not visible.

Sub testi2()
    'Bit waisty way to do it, you could just make a variable to hold the value -
    Dim myValue As Variant
    myValue = ActiveCell.Value
    'Sheets("sheets1").Select
    'Selection.Copy
    Sheets("pivot").Select
    'Range("C1").Select
    'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'Your choise tough, if you really need to copy the value to the cell C1 then by all 
    'means do, but you should still send the value to variable for code will be easier 
    'to be handled and clearer to read.
    'Here you could also clear all past filters for the pivot if needed. 
    'I won't encourage to but if there are other filters present exept
    'what is in filterWBS field, the code will run into an error. 

    Dim pItem As PivotItem
    Dim ifFound As Boolean
    ifFound = False

    'loop trough the pivotfieldvalues to see if match exists, pivot tables have a need for at least one visible value.
    For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems

        'if data exists then ifFound value will be set to true
        If pItem = myValue Then
            ifFound = True
        End If
    Next

    'based on the if value found set fields visible or hidden
    If ifFound = True Then
        For Each pItem In ActiveSheet.PivotTables("pivottable1").PivotFields("filter WBS").PivotItems
            If pItem <> myValue Then
                pItem.Visible = False
            Else
                pItem.Visible = True
            End If
        Next
    'if the value was not present show the message box
    Else
        MsgBox ("the value doesn't exists in the pivot")
        'You could in this case clear the filter
    End If
End Sub
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top