VBA code in Excel with pivot tables - vba

The data source will change often. After I refresh the pivot table, I have the following code to update the grade pivot item field. Sometimes grade 4 is available and sometimes it is not. Essentially, If grade 4 is available I want it to be selected and if it is not available then all fields can be select. For some reason when I run it, it stops on the else line. Any suggestions?
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").ClearAllFilters
If IsError(ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4") Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = _
"(All)"
Else
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4"
End If

You can't trap the PivotField("Grade").CurrentPage error with IsError.
Try the code below:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").ClearAllFilters
On Error Resume Next
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = "4"
If Err.Number <> 0 Then
ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade").CurrentPage = _
"(All)"
On Error GoTo 0
End If
Note: you could clean up your code if you use With statements. In your code, you could use With ActiveSheet.PivotTables("PivotTable1").PivotFields("Grade")

Related

EXCEL 2013 VBA filtering issue

Dear Stackoverflow users,
I am having difficulty with something that seems simple enough to not cause this much trouble. I am trying to filter a Pivot Table based on the greater than equal to criteria, mentioned in the code below. Here is the code:
Sub KEEP_ON_FILE_DT_CLICK()
Dim caches As Excel.SlicerCaches
Set cache = ActiveWorkbook.SlicerCaches("Slicer_KEEP_ON_FILE_DT")
With cache
For i = 1 To .SlicerItems.Count
If .SlicerItems(i).Selected Then
SelectedItem = .SlicerItems(i).Value
Exit For
End If
Next i
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT"). _
PivotFilters.Add Type:=xlValueIsGreaterThanOrEqualTo, Value1:=SelectedItem
End Sub
For some reason, the last line where I actually apply the filter keeps giving me the following error message:
Run time error '5':
Invalid Procedure Call or argument
Kindly assist.
Thanks
I assume that your pivot table does not allow for any such filters to be applied. There are many different ways / types of pivot tables and not all of them allow for such filters as xlValueIsGreaterThanOrEqualTo.
Yet, the following code should work under all circumstances:
Dim itmList As PivotItem
Dim dblDesiredValue as Long
dblDesiredValue = 32
ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT").EnableMultiplePageItems = True
For Each itmList In ActiveSheet.PivotTables("PivotTable2").PivotFields("KEEP_ON_FILE_DT").PivotItems
Select Case CDbl(itmList.Name)
Case Is >= dblDesiredValue
itmList.Visible = True
Case Else
itmList.Visible = False
End Select
Next itmList

Excel VBA: Detecting empty cell to end loop

In Excel 2007 I have imported data consisting of a column of rows. I wish to set a VBA script to read through the data from top to bottom, using a loop. The loop should continue to iterate while the cell in the sixth column is not empty.
I have tried:
Do While IsEmpty(Cells(i,6)) = False
...
i = i + 1
Loop
...and also
Do While Cells(i, 6).Value <> ""
...
i = i + 1
Loop
The loop does what it is supposed to but an error ensues because the program goes on reading below the block of data. (I inserted "On Error GoTo Catch" in the loop, then after the loop "Catch: MsgBox Cells(i, 6).Value & "Error" so as to confirm that an error occurs).
There is nothing in the loop that would affect Cells(i,6).
There are many questions in Stack Overflow about empty cells but the answers tend to avoid the issue how to detect an empty cell to make a loop end. Is there a way of doing this?
Provided you're on the right sheet (replace below) the code you posted should work
i = 1
Do While sheets("sheet1").Cells(i, 6).Value <> ""
i = i + 1
Loop
Could you post a screenshot of the column end? Your error handling may be preventing the error message too, try disabling that temporarily. Do the columns all end on a certain row? You could specify "do while not blank AND row < end row".

Next without For error in Select Case

I'm trying to write a macro that processes data but skips over data points that meet specific criteria. Here's a simplified version of my code:
Set gData = ThisWorkbook.Sheets("Dashboard").Range("E3:E16")
For Each cell In gData
Select Case cell.Value
Case Is = 20
Next cell 'error thrown here
Case Is > 20
'Do stuff
End Select
Next
The compiler is throwing a "Next without For" error in the location noted above. I haven't been able to find a working solution to this problem. Please help!
Set gData = ThisWorkbook.Sheets("Dashboard").Range("E3:E16")
For Each cell In gData
Select Case cell.Value
Case Is > 20
'Do stuff
End Select
Next

Error when setting VBA pivotfields.currentpage

I am trying to make a macro that will automaticly adjust the filters for several pivot tables based on user input, however if the user puts in something that isn't avaialble the code produces an error when trying to apply the filter.
Is there anyway to check which filters are available to select?
Example: One pivot table has three filters available (Year, Month, Type [Complaint, Praise, Both]) but if during a month there weren't any complaints then there is an error.
Code:
With PTable
.PivotFields("Year").CurrentPage = Y
.PivotFields("Month").CurrentPage = M
.PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
End With
Further to my comments, try this
With PTable
.PivotFields("Year").CurrentPage = Y
.PivotFields("Month").CurrentPage = M
On Error Resume Next
.PivotFields("Type").CurrentPage = T 'Error line if T isn't valid
If Err.Number <> 0 Then
Msgbox "Filter Didn't get applied"
End If
On Error GoTo 0
End With

Runtime error "424: Object Required"

I am having difficulties with this code. I am trying to make it so that based on the value of cell D25 the value of F25 will change. I made the code in VBA (don’t know if this is actually the best way) and it is giving me a 424 runtime error: Object Required. Could someone please point me in the right direction?
Sub Storage_vesel_Controle()
Sheets("NSR Form").Select
If Range("D25") = "1" Then Range("F25").Select.Value = "0"
If Range("D25") = "2" Then Range("F25").Select.Value = ".95"
If Range("D25") = "3" Then Range("F25").Select.Paste = ".98"
End Sub
Also, what do I need to add to make the code "always running"... in loop I think?
Further to my comment above, I wasn't planning on posting an answer but saw few things which I think I will bring to your notice and comments wouldn't accommodate so much of text.
I would recommend not using .Select to select the sheet but work with the sheet directly (See Below code) You might also want to see this link
Sub Storage_vesel_Controle()
With Sheets("NSR Form")
Select Case .Range("D25").Value
Case 1: .Range("F25").Value = 0
Case 2: .Range("F25").Value = 0.95
Case 3: .Range("F25").Value = 0.98
End Select
End With
End Sub
EDIT:
Regarding your edit. You can use the Worksheet_Change Event to ensure that the values of F25 gets automatically updated or if you want a non VBA solution then simply use a Formula in F25
If you are interested in Worksheet_Change then see this. Else a simple formula like this in F25 will suffice your needs :)
=IF(D25=1,0,IF(D25=2,0.95,IF(D25=3, 0.98,"")))
Clean it up a little bit. Using a Select Case statement will be easier to work with than mutliple If/Then statements.
Sub Storage_vesel_Controle()
With Sheets("NSR Form")
.Activate '<this line is not actually necessary unless you want to be on the sheet.
Select Case .Range("D25").Value
Case 1
.Range("F25").Value = 0
Case 2
.Range("F25").Value = 0.95
Case 3
.Range("F25").Paste = 0.98
Case Else
'do nothing, or, modify as needed
End Select
End With
End Sub