Filtering Pivot Table column - vba

I tried and looked on this forum and Others but I couldn't find the right working code yet.
I want to filter on a pivot table a column with values equal or higher than 10%.
When I recorded my macro, code was as follow:
Sub Macro9()
Macro9 Macro
ActiveSheet.Range("$A$5:$M$36607").AutoFilter Field:=13, Criteria1:=">=0.1" _
, Operator:=xlAnd
End Sub
This doesn't work but I wanted to launch the macro afterwards.
So I tried other codes, like the following :
Sub FilterPivotTable()
Application.ScreenUpdating = False
ActiveSheet.PivotTables("PivotTable1").ManualUpdate = True
ActiveSheet.PivotTables("PivotTable1").PivotFields("Name of my column").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Name of my column").PivotFilters. _
Add Type:=xlCaptionEquals, Value1:=">=0.1"
ActiveSheet.PivotTables("PivotTable1").ManualUpdate = False
Application.ScreenUpdating = True
End Sub
But this didn't work either.
Can anyone please help me?
Thank you very much in advance.
EDIT: Apparently, the fact that my field is a calculated one is important. Still don't have an answer though.

So, I found myself an answer to my question.
Since the columns I wanted to filter were values and calculated fields, I actually needed to filter these columns by right-clicking on a cell inside a row value of my PVT. Then, I had to filter by chosing the right value to sort and add my criteria.
The code when recording a macro is as follow:
ActiveSheet.PivotTables("PivotTable1").ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("Your row name").PivotFilters. _
Add2 Type:=xlValueIsGreaterThanOrEqualTo, DataField:=ActiveSheet. _
PivotTables("PivotTable1").PivotFields("Your value field"), Value1:=0.1
Hope it can help some of you, as I haven't seen an answer to this issue in many forums.
Have a nice day!

Related

Autofiltering two columns as an OR function

I am trying to filter two columns in excel to show all the results that deliver today(two separate columns). I have tried multiple ways, however I keep getting results that reflect an "AND" statement. I feel like I am close with.
Sub Playing_Today_v2()
Dim s As String
s = "=" & CStr(Date)
With ActiveSheet.ListObjects("table1").Range
.AutoFilter
.AutoFilter Field:=7, Criteria1:=s
End With
With ActiveSheet.ListObjects("table1").Range
.AutoFilter
.AutoFilter Field:=9, Criteria1:=s
End With
End Sub
Any and all help is greatly appreciated!!
Hello in order to do this via VBA you will first need to understand how to accomplish it manually 2 ways that come to mind are:
Add a helper column to the table
Use an advanced filter
For the helper column add a formula similar to =OR(Col7=TODAY(),Col9=TODAY()) and filter to that column equals true.
For the advanced filter create a 2 x 3 range and populate as follows:
Col7 NAME, Col9 NAME
=Today() ,
, =Today()
As use that as your criteria range to do a OR selection (if you want an AND selection put them on the same line).
Once you have got this working manually it is fairly easy to move to VBA either by adding a new column and adding 1 autofiler similar to your existing code or by using the Range.AdvancedFilter method.
If you get stuck with the code please post a question showing how far you get and someone will be able to help.

Excel macro to normalize data

I am currently trying to create a macro for Excel in which a column containing certain values (numbers basically) will be displayed in a new column in a normalized way (highest number = 1, lowest number = 0).
Usually, I would just use the formula:
=(J2-MIN($J$2:$J$XXX))/(MAX($J$2:$J$XXX)-MIN($J$2:$J$XXX))
However, as the length of the column is dynamic and will change for each set of values, I cannot enter a value for XXX.
Now, I found out how to have a dynamic range (e.g.: numRows = Range(Selection, Selection.End(xlDown)).Rows.Count) but I did not manage to merge both functions.
I found a thread already in this site about normalization of data but I think it was a bit of a different story and this one here should be simpler.
I would appreciate any help! As I just started working with macros (2h ago) I would also appreciate if this will be in simple language.
EDIT:
First of all, thanks for the quick reply!
I naively tried making it work with this code:
Sub Normalize_TEST()
'
' Normalize_TEST Makro
'
'
Range("A1").Select
numRows = Range(Selection, Selection.End(xlDown)).Rows.Count
Range("K2").Select
ActiveCell.FormulaR1C1 = "=(("J2")-MIN($J$2:$J$numRows$))/((MAX($J$2:$J$numRows$)-MIN($J$2:$J$numRows$))
Range("K2").Select
Selection.AutoFill Destination:=Range(Cells(2, 11), Cells(numRows, 11))
End Sub
But it is not working and I get an error message ("error of compiling").
I just realize now that you are absolutely right, I don't even need VBA. Your line of =(J2-MIN($J:$J))/(MAX($J:$J)-MIN($J:$J)) works fine. I wasn't aware that with $j:$J it realizes to not include empty cells.
I simply used this code now for cell K2 and then did a VBA autofill function for the rest.
I think this can be closed.
Thank you #tigeravatar for your super quick help!

Why is my auto fill not working?

Hoping you can help with the below, I've no idea why this isn't working and I can't seem to figure it out. After some Googling, I can't even find another example of this issue.
Essentially the code should take data on one page, place HLOOKUPS in another page to sort everything into the right columns (all working fine). Then once that's done, it should auto fill down using the row count.
The problem I have, is that it is auto filling on the wrong sheet (it might be worth calling out that the sheet it fills is the same one the code is in and where the rowcnt is.
I tried to explicitly call out the sheet I want to use as such: Range("A2:V2").AutoFill Destination:=Sheets(5).Range("A3:V" & rowcnt), Type:=xlFillDefault but this then throws an Application-defined or object-defined error on the fill line of code.
Public Sub FormatData()
rowcnt = Application.WorksheetFunction.CountA(Sheet4.Range("B:B")) + 1
With Sheets("Final Datasets")
.Cells(2, "A").FormulaR1C1 = _
"=HLOOKUP(""oOrder_date"",'Teradata Downloads'!R1:R1048576,ROW('Final Datasets'!RC),0)"
[snip] load more of the same as above [/snip]
'FILL
Range("A2:V2").AutoFill Destination:=Range("A3:V" & rowcnt), Type:=xlFillDefault
End With
End Sub
I'm properly puzzled here, so any help you can give as to how to fix this (and more importantly, why it's happening) would be greatly appreciated.
if you have the AutoFill in the With Statement, try to use the "." before the Range.
.Range("A2:V2").AutoFill Destination:=.Range("A3:V" & rowcnt)

Using a function to return a value in VBA without using the worksheet

Good afternoon One and All,
I am relatively new to VBA and I am trying to use the Worksheet function, specifically the index function to look up information in the code and bring back a value. I would love to do this without having to assign it to a cell in the worksheet using R1C1. Is there a way to do Vlookups, or Indexex without having to make assignments in the worksheet?
The examples below works as an equation placed within the sheet, but I'd like to get the same answer without having to use the sheet.
The first formula looks up a Batch number
The second formula is finding the name of the first ingredient in the batch, based on the SKU in the worksheet.
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[-1],'U:\Files\Maintenance File.xls'!SKUinfo,16,FALSE)"
ActiveCell.FormulaR1C1 = _
"=INDEX('U:\Files\[Recipe File.xlsx]Fresh 2800'!Fresh2800,2,6)*(INDEX(SchedInfo,MATCH(RC[-3],SKULookup,0),6))"
Any help would be appreciated, and thank you for all your help in advance.
The answer to this Microsoft forum post describes what you want. Use the Application object:
ans = Application.VLookup(arg1, MyRange, arg3, arg4)

How to determine the last column(last column with value) using VBA in excel?

I have a question wonder if anyone can help me out.
I'm doing a project that requires to get a summary of tests result. My flow is as follow,
First, I would need to open my collected test results, then creating a pivot table, after setting accordingly to what i need to view, i will copy the table into another spreadsheet. Over at this spreadsheet, it will change some of the names to the required names and finally, it will be copied to the summary sheet. Then i will need to tabulate the summary in % also.
I'm facing with an issue here which is, for my collected test results, when i put it into the pivot table, i cant determine the number of columns that will appear. For example
In on example, we can have
In another case, we can have
If u notice the second image has more columns used.
Therefore, I was wondering if there is any way that you guys can share with me to determine the last available column with details to be copied.
And can i get the number of the last column so that i can get the summary as well since i need to put it in the form of %.
At the moment, i'm using "Range(A1:Z1000000)" which is quite impossible for me to do anything as i cant really find the % manually.
btw, i need it in VBA code.
Will appreciate for all the opinions and options provided! Thank you in advance!
This should do the trick:
Sub test()
Dim maxColumn As Long
'Create the table here
maxColumn = getMaxUsedColumnNumber("PivotTableSheet")
'do what you need with the number
End Sub
Function getMaxUsedColumnNumber(sheetName As String) As Long
With Sheets(sheetName)
getMaxUsedColumnNumber = .Cells.Find(What:="*", after:=.Range("A1"), LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
End With
End Function
You can use .SpecialCells() as follows:
ActiveSheet.UsedRange.SpecialCells(xlLastCell).Column
Alternatively, take a look at the ColumnRange property of the PivotTable object. It has a Count property which will return the number of columns in the Pivot Table which could also help.