Pivot Table Changing Cell Colors - excel

I am hoping someone can help me figure out a really annoying aesthetic feature of pivot tables in Excel.
Here is what my current "analysis" screen looks like without the pivot tables:
However, I have placed pivot tables in the black boxes and have a macro to auto update them after I import more data. That is below:
Sub UpdatePivots()
' This sub is intended to update all pivot charts in the by switching to the appropriate
' worksheet, locating the appropriate pivot table, and updating them.
Dim ws As Worksheet
Dim PT As PivotTable
Dim pvtItem As PivotItem
For Each ws In ActiveWorkbook.Worksheets '<~~ Loop all worksheets in workbook
For Each PT In ws.PivotTables '<~~ Loop all pivot tables in worksheet
PT.PivotCache.Refresh
Next PT
Next ws
End Sub
However, the imported data changes. Sometimes it is very small, sometimes it is very large, and so I end up with my pivot tables looking like this:
How is it possible for me to either write code, or change settings, so those ugly white rows don't appear? I would like for the gray to remain if there are no pivot rows. I have tried checking in Pivot Table formatting settings but have been unsuccessful.

You can use a standard last row identifier and then color your interior cells below. You will need to pick a max row number (here it always color form last row down to 100, so change 100 if needed). You will also need to set the columns since you have a black border.
Last, you will just need to add the color code. I added a bit of code below to extract the color code if you don't know what it already.
This solution will work for the left pivot. You will need to duplicate the two lines of code I added for the pivot on right.
Sub UpdatePivots()
Dim ws As Worksheet
Dim PT As PivotTable, pvtItem As PivotItem
Dim LRow As Long
For Each ws In ActiveWorkbook.Worksheets
For Each PT In ws.PivotTables
PT.PivotCache.Refresh
LRow = ws.Range("D" & ws.Rows.Count).End(xlUp).Offset(1).Row
ws.Range(ws.Cells(LRow, "D"), ws.Cells(100, "G")).Interior.Color = 11184814
Next PT
Next ws
End Sub
If you do not know the color code, you can run the below code to find it!
Sub ColorCode()
Dim Target As Range
Set Target = Application.InputBox("Select desired cell to return color code", Type:=8)
If Not Target.Count > 1 Then
MsgBox "Color Code: " & Target.Interior.Color
End If
End Sub

Related

Resize table using Listobjects not working

I have a table in Sheet 2 with a name "MyTable". Number of rows of that table changes each time depending on the data. I would like to clear the contents of the table and resize it using a macro so that it has only two rows- a title row, and an empty row.
Table title row is from B5 until K5.
I tried the below code, it clears the table contents and resizes, however, does not resize as desired. It resizes, without clearing the table borders in column C.
Any help is really appreciated.
Sub Table_Resize()
Dim rng as Range
Sheet2.Select
Range("MyTable").ClearContents
Set rng = Range("MyTable[#All]").Resize(2, 10)
Sheet2.ListObjects("MyTable").Resize rng
End Sub
I think that what you are trying to do is to delete the all rows.
Sub Table_ClearContents_Resize()
Dim ws As Worksheet: Set ws = Sheets("Sheet2")
Dim ol As ListObject: Set ol = ws.ListObjects("MyTable")
' Delete table contents
ol.DataBodyRange.ClearContents
' Resize table
ol.Resize Range(ol.HeaderRowRange.Resize(2).Address)
End Sub

How to get the range of "Total Sum" column in a pivot table? Excel VBA

Is there anyway to get the range of the total sum column from a pivot table using excel vba. The range I am referring to is in the screenshot below, highlighted yellow.
I tried using the macro recorder in excel to see if it could help answer my question. This results in me getting this.
ActiveSheet.PivotTables("PivotTable1").PivotSelect _
"'Sum of Unit Cost' 'Row Grand Total'", xlDataAndLabel, True
however this selects a range which is more than needed as seen in the screenshot.
I could do something like offsetting what is selected by 2 rows and then maybe resizing it to fit the intended range but I was wondering if there was a more straight forward way of doing this.
You can use this code to select GrandTotals. It works for rows and columns in case you also need it. The final part is to remove last row (or column).
Sub SelectGrandTotal()
Dim pt As PivotTable
Dim rColumnTotal As Range, rRowTotal As Range
Dim numrows As Long, numcolumns As Integer
Set pt = ActiveSheet.PivotTables(1)
With pt
'The conditions below are checking if the GrandTotals are activated. Not really necessary in some cases.
'Uncomment this block to work with Columns
'If .ColumnGrand Then
' With .DataBodyRange
' Set rColumnTotal = .Rows(.Rows.Count)
' rColumnTotal.Select
' End With
'End If
If .RowGrand Then
With .DataBodyRange
Set rRowTotal = .Columns(.Columns.Count)
rRowTotal.Select
End With
End If
End With
'Resizes selection and removes last Row (you can do the same for columns if necessary)
numrows = Selection.Rows.Count
numcolumns = Selection.Columns.Count
Selection.Resize(numrows - 1, numcolumns).Select
End Sub
Try changing xlDataAndLabel to xlDataOnly; see the XlPTSelectionMode enum on docs.microsoft or find it in the Object Browser (F2) for a list of all the available members.

Create a chart for each pivot table

In my active sheet I have two Pivot table (PivotTable1 and PivotTable2) sometimes they could be more though.
I am trying to add a chart for each PT in the active sheet. So far I came up with this code but it is giving me 4 charts 3 blank ones and a correct one if one of the PT is selected.
Sub CreateChart1()
Dim pivot As PivotTable, sh As Worksheet, nrp As Long
Set sh = ThisWorkbook.Sheets("Sheet1")
For nrp = 1 To sh.PivotTables.Count
Set pivot = ActiveSheet.PivotTables(nrp)
For Each pivot In sh.PivotTables
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=pivot.Parent.Name
Next pivot
Next nrp
End Sub
Can somebody explain to me what am I doing wrong?
Any help is appreciated.
Thanks
Nick.
You are getting four charts even though you only have two pivot tables because you have two loops and only need one.
The inner loop For Each pivot In sh.PivotTables loops through each pivot table, as does the outer loop For nrp = 1 To sh.PivotTables.Count. So you'll get 2 charts for each pivot table.
Instead of using Charts.Add I suggest using ChartObjects.Add, which creates an embedded chart and where you can control the placement and size.
Then you also need to SetSourceData.
An example might look like this:
Sub AddPivotChart()
Dim pivotTbl As PivotTable, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet2")
For Each pivotTbl In ws.PivotTables
Dim chtObj As ChartObject
Set chtObj = ws.ChartObjects.Add(50, 50, 200, 200) ' adjust as needed
chtObj.Name = pivotTbl.Name
chtObj.Chart.SetSourceData pivotTbl.TableRange1
Next pivotTbl
End Sub

Excel VBA - Hide range of rows after variable sized pivot table

I trying to write a macro to format a number of sheets containing pivot tables. I am stuck on this one problem.
My sheet contains a number of stacked pivot tables. I need the VBA code snippet to hide, say, 1000 rows after the first table (or all tables except the top one if possible). The top table will vary in size from one day to the next so it is not possible to just hide a set range.
Thanks
To hide all the pivot tables except one, you may try something like this...
In the below code, change the name of the pivot table you don't want to hide.
Sub HideAllPivotTablesButOne()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
If pt.Name <> "PivotTable1" Then 'Name of the Pivot Table which you don't want to hide
pt.TableRange2.EntireRow.Hidden = True
End If
Next pt
Application.ScreenUpdating = True
End Sub
If you want to show all the pivot tables again, you may try the below code...
Sub ShowAllPivotTables()
Dim ws As Worksheet
Dim pt As PivotTable
Application.ScreenUpdating = False
Set ws = ActiveSheet
lr = ws.UsedRange.Rows.Count
For Each pt In ws.PivotTables
pt.TableRange2.EntireRow.Hidden = False
Next pt
Application.ScreenUpdating = True
End Sub

How do you show hidden rows in Excel chart, but NOT hidden columns?

Is there a way to enable an Excel chart to plot data in hidden rows, but NOT in hidden columns? I already know how to use the "Select data" option, when right clicking a chart, to go to the "Hidden and empty cells" option, which offers the choice to "Show data in hidden rows and columns". I can't find a way, however, to show data in hidden rows without also showing data in hidden columns and was hoping someone might be able to suggest a VBA solution.
Many thanks,
Geoff
I just drummed this up for you. As a caveat, working with charts can be very tricky. This code will only work with certain types of charts, and you may have to make some adjustments to work with your particular data set.
Option Explicit
Sub RemoveHiddenColumns()
Dim myChart As ChartObject
Set myChart = ActiveSheet.ChartObjects("Chart 1") 'place in here whichever chart you need to reference, asssumes the chart is on the activesheet
myChart.Activate 'first activate the chart
Dim i As Integer
For i = 1 To ActiveChart.SeriesCollection.Count 'loop through each series
Dim strText As String, strCol As String, strSht As String, intCol As Integer
strText = Split(ActiveChart.SeriesCollection(i).Formula, ",")(2) 'extract sheet name and column of series
strSht = Split(strText, "!")(0) 'get sheet name of series
strCol = Split(strText, "!")(1) 'get column range of series
Dim wks As Worksheet
Set wks = Worksheets(strSht)
If wks.Range(strCol).EntireColumn.Hidden = True Then 'if the column is hidden
ActiveChart.SeriesCollection(i).Delete 'remove the series
End If
Next
End Sub

Resources