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
Related
I am trying to use Excel VBA to count the number of rows until I hit a specific string and set a variable equal to it.
Something like:
Dim i as Integer
i = Worksheets("Scope Phase Document").Range("A1", Range("A1").End(xlDown)).Find("FACTS - What are we measuring?").Count
I know this isn't the correct syntax and I'm probably missing other stuff, but just using the different functions I currently know, this is what I would hope would do the trick. I get
Run-time error '91' saying "Object variable or With block variable not
set
I have tried a few different ways of doing it, but can't figure out a way that doesn't result in an error.
So I want to start at A1 and count all the rows down until I reach the specific string "FACTS - What are we measuring?".
Any help would be greatly appreciated!
I prefer MATCH, but if the match is not found it throws an error. So we need to test for that:
Dim i As Long
i = 0
On Error Resume Next
i = Application.WorksheetFunction.Match("FACTS - What are we measuring?", ActiveSheet.Range("A:A"), 0)
On Error GoTo 0
If i > 0 Then
' do you stuff with i
End If
So you basically want MATCH():
=MATCH("FACTS - What are we measuring?",A:A,0)
It returns the row number of matched string.
Your code is fine except you should use the Row property. The Count property as you have used it will return 1 because the Find method returns one cell (the first cell where a match is found).
Dim i as Integer
i = Worksheets("Scope Phase Document").Range("A1", Range("A1").End(xlDown)).Find("FACTS - What are we measuring?").Row
Like Scott mentioned, if your text is not found, Excel will throw an error.
I'd do this:
Sub rowcounter()
Dim i As Integer
Range("A1").Select
i = 0
Do
Selection.Offset(1, 0).Select
i = i + 1
Loop Until Selection.Value = "FACTS - What are we measuring?"
MsgBox "rows count is " & i
End Sub
the code is intended to seek out the "Yes" in "OverExtensions" and will create a new range and determine the maximum value of the new range. For some reason the logic of the code seems alright but I'm getting an empty result and would love the community's input as to where I've went wrong.
Private Sub CommandButton1_Click()
Dim maximum As Double, OverExtension As Range, x As Range
Cells.Interior.ColorIndex = 0
Set OverExtension = Range("D2:D12")
maximum = WorksheetFunction.Max(Range("c2:c12"))
For Each x In OverExtension
If x.Value = Yes Then
If x.Offset(0, -1).Value = maximum Then
x.Cells.Interior.ColorIndex = 22
End If
End If
Next x
End Sub
You nested the if and for blocks in a wrong way.
You have to put the second End If between Next PriceNo and Next OEResult.
Update:
You need just one End If. The first one is wrong because If and Then are in one line.
Besides you have problem with your OEYes and OEResult variables as they are defined but not initialized but as I don't know your intention I also don't know how to solve it.
I recorded this macro to update the date range of 16 charts. But, as you can see I am getting a Run-time error.
I have looked at other threads on stackoverflow that relate to this but none come close. The darn help button on excel doesn't help either. Can you please advise? Here is the code:
ActiveSheet.ChartObjects("Chart 18").Activate
ActiveChart.Axes(xlCategory).Select
ActiveSheet.ChartObjects("Chart 18").Activate
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Date")
.PivotItems("Sep-15").Visible = False
.PivotItems("Aug-14").Visible = True
End With
Here's some simple code that should get you close. You will need to change "MySheet" in this code to the name of the sheet containing the pivot table in your workbook, and your Date field must truly be text formatted in the "Mmm-YY" format.
Sub ShowThirteenDatesStartingLastMonth()
Sheets("MySheet").PivotTables("PivotTable2").PivotCache.Refresh
Dim Dt As String
With Sheets("MySheet").PivotTables("PivotTable2").PivotFields("Date")
For h = 1 To .PivotItems.Count - 1
On Error Resume Next
.PivotItems(h).Visible = False
Next h
For i = 1 To 13
On Error Resume Next
Dt = Format(DateSerial(Year(Date), Month(Date) - i, 1), "Mmm-YY")
.PivotItems(Dt).Visible = True
Next i
End With
End Sub
It's hard to know without an example of your workbook, but I would try naming the sheet rather than using "activesheet". It's also typically a bad idea to activate or select anything, and instead you should allow the code to do as much work as it can without selecting anything.
Something like this might work better:
With Sheets("MySheet").PivotTables("PivotTable2").PivotFields("Date")
.PivotItems("Sep-15").Visible = False
.PivotItems("Aug-14").Visible = True
End With
As Josh said, you should reference the exact sheet name and maybe even the exact Pivot table name.
Also, you should avoid using .Select and instead declare a variable for each pivot chart, for example.
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
Within Excel, I use tables to store dynamic data inside a seperate worksheet. Entering new data works like a charm, however, I would like to be able to dynamically retrieve a single row from that table and store its data in variables. I would prefer to build a function so I could do something like this:
findFromCatsByDate(searchterm) 'returns a single row if found with mathing date.
Note that the table is dynamic and not a fixed range (so it changes vertically). I want to reuse this function with slight modification on other tables. I kind of need an example how to achieve this in VBA.
Thanks,
This will return a reference the row that matches Key in a specified table
Function GetRow(TableName As String, ColumnNum As Long, Key As Variant) As Range
On Error Resume Next
Set GetRow = Range(TableName) _
.Rows(WorksheetFunction.Match(Key, Range(TableName).Columns(ColumnNum), 0))
If Err.Number <> 0 Then
Err.Clear
Set GetRow = Nothing
End If
End Function
Example use
Sub zx()
Dim r As Range
Set r = GetRow("MyTable", 1, 2)
If Not r Is Nothing Then
r.Select
End If
End Sub