Excel VBA Pivot Table Hide Fields with less than 5 characters - vba

My PC is a Mac and I am using Excel version 16.54. What I am trying to filter out are all the zips that have < 5 digits but can't quite get it. I presently have the filter removing my N/A values like so:
Set objField = objTable.PivotFields("Zip")
With objField
objField.PivotItems("N/A").Visible = False
End With
Thanks in advance for any help!

Related

Decimal separator issue from SAP to Excel: "1,056" versus "1.056" (factor 1000)

I have written a VBA code in excel which is linked with SAP and it downloads the table VBRP in an excel sheet.
I have a problem with numbers. For example the billed quantity is 1.056 in SAP, when macro runs and trying to paste the value in excel then we have the number 1,056 not the one thousand and 56.
When I load the table by VBA and the copy paste manual the table in excel using Ctrl + V then I have the correct number 1.056 one thousand and 56 items.
What should I do in order to have the correct numbers in my excel?
you could remove the dots before inserting to the sheet. The number format set on your column will than be applied independent of the regional settings of the used machine. Something like this:
Sub testdecimalsep()
Dim test
test = Split("1.11, 1.00001, 5.333", ",")
For i = 0 To UBound(test)
test(i) = CLng(Replace(test(i), ".", ""))
Next i
Sheet1.Range("A1:A3").Value2 = Application.Transpose(test)
End Sub
best of luck,

I want to hide a column in excel using VBA macro in run time but while executing other columns also get selected.

I want to hide column C based on value in cell AB7.
If value in cell AB7 is 117 then entire column C needs to hide.
Else I want column C to be there.
If Range("AB7").Value = "117" Then
Columns("C:C").Select
Selection.EntireColumn.Hidden = True
Else
Columns("C:C").EntireColumn.Hidden = False
End If
When I run the code columns A:G also get selected. Please let me know if there are any limitations while using the above code. I use excel 2010. Thanks in advance.
I would recommend not using select. Columns("C:C").Hidden = False and Columns("C:C").Hidden = True

Excel Macro To Advance Date

I have a ton of excel sheets that each have 3 excel workbook tabs. On the last one there will be a ton of data but one column will be a date column with a bunch of different dates underneath. The date format will be MM/DD/YYYY. I need to advance each date ahead by 4 years.
I imagine that I will need to select the correct workbook, search for the particular column, and then loop to iterate through each value underneath that column to advance it, but the day itself needs to stay the same. For example, if its 10/05/2017, it needs to be 10/05/2021. Any suggestions or help would be great. Thank you in advance.
Thank you for the help, I realize I wasn't very helpful at all with my question. I'm very new to VB script and excel macros in general. I hadn't gotten how to search for the column itself as I would like it find the column no matter what the column value is (possibly search for a cell that says "Date" through the entire sheet?, I was just trying to add the 4 years to start with and couldn't find the function I needed. This is what I had from what I derived and seems like this is very wrong ha.
Do Until IsEmpty(ActiveCell)
Set ThisCell = ActiveCell
ThisCell = DateAdd("yyyy", 4, ColumnValueHere)
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
Loop
You'll be wanting the DateAdd function
Try something like this:
Sub AddDates()
Dim lastRow as integer
Dim theSheetImWorkingOn as worksheet
Dim theColumnNumberForTheDates as integer
theColumnNumberForTheDates = 5 ' change this to be the column number you want
Set theSheetImWorkingOn = Sheets("Put your sheet name here")
lastRow = theSheetImWorkingOn.Cells(1000000, theColumnNumberForTheDates ).End(xlUp).row
For x = 2 to lastRow ' assuming your data starts on row 2
theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates) = DateAdd("yyyy", 4, theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates))
Next x
End Sub

List visible pivot items with Excel formula

In an Excel pivot table, when I select multiple items in a report filter, Excel just displays that I have selected multiple items.
Data
I can select multiple items:
However, once the selection is done, I don't see which elements I have selected.
I want to display which items are selected, e.g. when the report is printed. I found a way to do this using VBA and a user defined function.
Function GetVisibleItems(FieldName As String) As String
Dim PivotTable As PivotTable
Set PivotTable = ActiveSheet.PivotTables(1)
Dim PivotField As PivotField
Set PivotField = PivotTable.PivotFields(FieldName)
Dim PivotItem As PivotItem
Dim Result As String
For Each PivotItem In PivotField.PivotItems()
If PivotItem.Name <> "(blank)" Then
If PivotItem.Visible Then
If Len(Result) > 0 Then Result = Result & ", "
Result = Result & PivotItem.Name
End If
End If
Next
GetVisibleItems = Result
End Function
Is it possible to get the same result using just Excel formulae, not VBA?
Martin - This probably isn't as straight forward as you are looking for, but using workbook functions to build the string you are looking for ...
Below is a simple table of information ...
... and a pivot table created from it ...
In the pivot, you can see that Person 2 and Person 6 are filtered out.
Back at the table, a column was added using this formula (filled down) ...
=IF(ISERROR(GETPIVOTDATA("Grade",Sheet5!$A$3,$A$1,A2)),"",A2)
Note: Sheet5 contains the Pivot table. A3 is the top left corner of the pivot.
Providing this result ...
Where there are blanks instead of Person 2 and Person 6.
The string was generated using Chip Pearson's StringConcat UDF because it's more compact than all the typing needed with & or CONCATENATE ...
What I was looking for is provided by Slicers. A slicer shows the range that has been selected and is printable. Writing the list of visible Pivot Elements into a cell is possible, maybe even with a tricky Excel formula, but seems like over-engineering it.
The TEXTJOIN function can do this. Excel 2016 onwards.
The TEXTJOIN function combines the text from multiple ranges and/or strings, and includes a delimiter you specify between each text value that will be combined. If the delimiter is an empty text string, this function will effectively concatenate the ranges.

Find and count the number of occurrences in a sheet

I need to find out how many times a sequence of numbers occurs in a sheet using VBA. For example:
201-1-55-8799
301-5-55-8799
202-1-55-8799
201-1-55-8799
999-5-55-8799
001-2-55-8799
I want to find out how many times 201-1 occur in this sheet. When you do FindAll in Excel it tells you at the bottom how many cells found.
I have experimented with CountIf but that only works if the cell just contains exactly 201-1.
The answer to the above search should be 2 instances of 201-1 found.
Then I need to write the number of occurances in a differnt sheet.
Thanks
Use the COUNTIF formula
=COUNTIF(A2:A12,"201-1*")
You could do this, using VBA
Dim tab_input as Variant
tab_input = ~your range~
specific_counter = 0
For i = 1 to Ubound(tab_input)
If Left(tab_input(i,1),5) = "201-1" Then
specific_counter = specific_counter + 1
End If
Next
msgbox specific_counter
That will count how many cells that have left text starting with 201-1 and and show in a box the amount.
I would recommend using Excel's own find function programmatically. Something like this:
http://www.ozgrid.com/VBA/find-method.htm