How to remove a blank row from slicer? - excel

I have a slicer which is used to manipulate a PivotTable. The PivotTable is a list of filters. Ultimately, the slicers + PivotTable are used to retrieve data and populate a worksheet for end users.
Issue
I recently added a combobox that autocompletes data so end users could quickly type what they were looking for rather than spend the time scrolling through each slicer and selecting appropriate options. As I have been testing the sheet, I noticed a blank row has appeared at the top of one of my slicers. How do I remove this blank row?
Attempts
I checked the data source for the PivotTable + slicers, there are no blank rows. I did a GoTo --> Blanks to try and isolate any blank cells (no blank cells in the data). I refreshed the data source and I also changed the data source to what it should be, the blank row in the slicer is still there, despite no blanks in my PivotTable data. I also went to Slicer Settings and checked 'Hide items with no data' but still, the blank entry in the slicer persists.
I would add that vba code that manipulates the PivotTable isn't modifying the slicers in any way. In my vba I call:
When I lock the sheet:
Me.Protect DrawingObjects:=False, UserInterfaceOnly:=True, AllowUsingPivotTables:=True
In the ComboBox1_Click() event after the user selects an entry from the autocomplete dropdown:
ActiveSheet.PivotTables("PivotTable").PivotFields("Portfolio").CurrentPage = _
ComboBox1.Value
And when a user clicks a 'Clear Filters' button I call:
ActiveWorkbook.ActiveSheet.PivotTables("PivotTable").ClearAllFilters
Those are the only manipulations of the PivotTable within my vba code.

Related

PivotTable Not Refreshing With Data

I have an Excel document with 4 Worksheets.
On Sheet1 I set my start & end dates for my data.
On Sheet2 is the data itself (provided by an external SQL database).
On Sheet3 & Sheet4 are PivotTables that use the second sheets data as their source.
I have created a VBA macro button on Sheet1 to refresh all.
I'll change my date range on Sheet1 then click the button but it only refreshes the data on Sheet2 and not the PivotTables on Sheet3 & Sheet4. I have to click the button a second time to refresh the PivotTables with the new data.
Here is the VBA code:
Sub Button2_Click()
Application.ScreenUpdating = False
ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
End Sub
Even manually using the Refresh All button has the same outcome.
It seems when you do a refresh all it is trying to refresh everything in parallel, probably to save time. However, I think since you have pivot tables which are dependent on data which needs to refresh from the DB, you have to ensure that your external data is refreshed prior to attempting to refresh the pivot tables.
I was able to reproduce the issue and doing the below resolved it for me. Just have to replace "Query1" with the name of your query.
ActiveSheet.ListObjects("Query1").QueryTable.Refresh BackgroundQuery:=False
For Each pc In ActiveWorkbook.PivotCaches
pc.Refresh
Next
Edit:
To get the query name, you can go to the "Data" tab on your ribbon you should see a button that says "Queries & Connections" clicking on that will expand a pane on the right side of the excel screen. In that pane clicking on the Queries tab on the top will give you a list of your queries that are set up
Screenshot of my QueryTable name

Excel VBA - Select first cell in filtered listobject data

I want my macro to select or goto the first row of filtered data in a listobject. I am trying this code but I get an error.
activesheet.AutoFilter.Range.offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 2).select
I would like to do this because if the user filters the data, whilst he has scrolled mid-way through the sheet, then he might not see all of the filtered data because of the freezed panes.
ok I figured this out:
Application.Goto lo_f.DataBodyRange.SpecialCells(xlCellTypeVisible).Cells(1), True
Need to use Goto in order to bring the screen to the filtered cell.

Macro to update filter on pivot to include all but "Blank" while leaving field settings as is

I need a macro to select the Row Label filter and select all but "Blank." The last condition is, I need the macro to leave field settings as is: namely, I need the "Show items with no data" to remain unchecked.
We tried .showallitems=true but this modified the field settings to show items with no data.
Also tried pivotitem.visible=true but nothing happened.
Fire up the Macro recorder, then clear the filter, and deselect blank. That will give you the code you need.
For a non OLAP PivotTable, it will be as simple as this:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("test")
.ClearAllFilters
.PivotItems("(blank)").Visible = False
End With

Macro button to copy link in pivot table based on Slicer

I'm starting with a Pivot Table that has all my data. One of the data fields is a URL string that I ultimately want to send my users to.
To make searching through the pivot table as easy as possible, I've got a couple slicer windows to let the users click their search criteria. What I'd like to do, is based upon their search criteria (once narrowed down to one result), is have a Macro Button that automatically launches their browser (lets say internet explorer) with the link found in the pivot table.
I can figure out the launching of the browser (or just a copy text), but is there a way that I can program the Macro Button's action based upon the selections of the Slicers that are associated with my pivot table?
Any help would be much appreciated!
If I understood you correctly, you want to launch your code when only one link appears in PivotTable (after you apply slicers to it).
You can achieve this with Worksheet_PivotTableChangeSync event and helper cell.
lets assume you have your pivot table in column G, starting from cell G2. Enter this this formula =COUNTA(G3:G20) in cell G1(adjust range to your needs).
then in your worksheet module (not regular module) enter this code.
Private Sub Worksheet_PivotTableChangeSync(ByVal Target As PivotTable)
Dim a As Long
a = Range("G1").Value
If a = 1 Then
'your code for link in cell G3
Else
End If
End Sub
Now when you slice your PivotTable to only one selection, COUNTA formula will evaluate to 1 and trigger Worksheet_PivotTableChangeSync event.

Adding unlocked rows into protected Excel sheet

I am trying to make an Excel sheet where I show previous records to people and make them add new records using 'add row' functionality.
So I am making my excel sheet protected so they can not tamper with older records. And I am keeping one row unlocked where they can start adding new information.
I also checked the 'Allow adding new rows' option in protection dialog box.
The problem is when I add a new row in the protected sheet, all the cells in that row are locked and I am unable to make an input through it.
Is there a workaround for this? Thanks.
Simply add row just below unlocked row to avoid the issue.
Using VBA
You can explicitly specify the locked=false for new rows.
Try below sample code.
Sub test()
ActiveSheet.Unprotect "test"
Rows(5).Insert
Rows(5).Locked = False
ActiveSheet.Protect "test"
End Sub
The easy workaround without using macros is as follows:
You need two unprotected rows
Hide the first one
Protect the sheet and allow insertion/deletion of rows (not formatting.. they could unhide the row)
People then can add rows via HOME -> Insert -> Insert Sheet Rows or delete them in the same way.
(people can't select the whole rows because of possible locked cells on the right and press [CTRL]+[+])
DONE!

Resources