Excel 2013. I have pivot table, which grows and shrinks according to the data rows. For example,
1. For the Row label, I have list of all products
2. For the Column label, I have list of manufacturers
3. For the Values, I have a count of 1 for the manufacturer. This would tell me how many manufacturers produces the same product.
Therefore the product list (row) grows and so does the Manufacturer list (columns) expands.
My question: Is there a way to get the range of the pivot, for example if the pivot tables starts at A1 and there are 10 products and 10 manufacturers, then the range of the pivot table would be $A$1:$K$12 (without Total for row or Total for column).
Is there a quick way to extract the range $A$1:$K$12?
Thank you in advance,
G.
To get the pivot table range that doesn't include the filters range, which is usually two rows above the table, use this code. This will select the actual pivot table range, and output the address of the range of the pivot table in a msg box.
Set pt = ActiveSheet.PivotTables(1)
pt.TableRange1.select
Msgbox pt.TableRange1.address
I see you tagged Excel-VBA.
Yes if you are able to consider Range as Range(cells(1,1),cells(12,12)) instead of Range("A1:K12")
There is a way to count field items in VBA.
Rcount = Sheets("YOUR SHEET NAME").PivotTables(1).PivotFields("YOUR ROW NAME").PivotItems.Count
Ccount = Sheets("YOUR SHEET NAME").PivotTables(1).PivotFields("YOUR COL NAME").PivotItems.Count
From there you would need only add 2 to compensate for extra rows/columns
Rcount = Rcount+2
Ccount = Ccount+2
Dim TableRNG as range
Set TableRNG = Range(cells(1,1),cells(Rcount,Ccount))
Hard to go any further without knowing what you are trying to accomplish and im not sure you were really asking for a VBA solution.
Related
So im currently working a table in excel that I have named Table1 with three columns (Column 1, Column 2 and Column 3). Ive been trying to count the used rows or populated rows inside the table using VBA but have had no luck.
Example 1:
UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").UsedRange.ListRows.Count
Example 2 (This One Returns only all available rows)
UsedRows= Sheets ("Sheet1").ListObjects.("Table1[#Column 1]").ListRows.Count
I either want the populated or unpolulated row amount. Either of the two will work just fine. Remember this is a Table so End(xlUp) and End(xlDown) work a little bit different. Ive tried those too but I still get either the total rows available or the cells that are modified which is way more than what I have available.
Thanks for the help in adavanced whoever posts.
Sounds like you can use CountA, like this perhaps:
Dim myColumn As ListColumn
Set myColumn = Sheets("Sheet1").ListObjects("Table1").ListColumns("Column 1")
Dim UsedRows As Long
UsedRows = Application.CountA(myColumn.DataBodyRange)
Debug.Print UsedRows
If you don't have blank cells in other rows. The 3 doesn't need to be hard-coded, this is just the number of columns in your table.
Sub x()
Dim r As Range
Set r = ActiveSheet.ListObjects(1).DataBodyRange
With WorksheetFunction
MsgBox .CountBlank(r) / 3 'empty rows
MsgBox (r.Rows.Count - .CountBlank(r) / 3) 'non-empty rows
End With
End Sub
I have a pivot table with data, I need to copy and past all of the data except for the last row, which I need to delete as it is a sum of totals. The amount of data entries vary by week so I cannot just put the set numbers in. Currently my code looks like this;
Worksheets("Mobile Summary").Range("A4:F482").Copy
Worksheets("Sheet1").Range("A1:F479")
However I need to copy and paste more data from a different pivot table directly under this. Therefore I couldn't just set the rows to 3000 for example. I know there is a code to find the last row, although I'm coming up short as to how to go about solving this problem.
To find a pivot table by its name and remove the last row of the table use the Range.Resize property. This is useful if there is more than one pivot table on a worksheet.
Option Explicit
Sub CopyPivotTableWithoutLastRow()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Tabelle3")
Dim i As Long
For i = 1 To ws.PivotTables.Count 'loop throug all pivot tables
If ws.PivotTables(i).name = "PivotTable1" Then 'determine pivot table by its name
With ws.PivotTables(i).TableRange1 '= full table
.Resize(RowSize:=.Rows.Count - 1).Copy 'resize to remove last row then copy
End With
End If
Next i
End Sub
I have an Excel document which pulls data from an Access database.
From here, the data is summarised in a Pivot Table.
It is then pulled into another table which makes it easier to read and filter.
The last table is formatted as a table and formulas are in place.
Depending on the data in the database, this table can reduce or expand the number of rows when refreshed.
When I run the macro to refresh the data and tables, I want to be able to automatically resize the table so all of the data is shown, but no extra blank rows appear at the bottom.
So far, I have the following code which looks up the Pivot Table sheet (Pivot) to determine the number of rows to display in the output sheet (Report):
Sub ResizeList()
Dim ws As Worksheet
Dim ob As ListObject
Dim Lrow1 As Long
Lrow1 = Sheets("Pivot").Cells(Rows.Count, "A").End(xlUp).Row
Set ws = ActiveWorkbook.Worksheets("Report")
Set ob = ws.ListObjects("Report_Table")
ob.Resize ob.Range.Resize(Lrow1)
End Sub
However, it only removes the table formatting (not the data) from the extra rows at the bottom when the table reduces in size.
Also, I get too many rows in the Report Table because of the header and total rows in the pivot.
Can someone help?
I am trying to create a Stacked Column Chart with the data in the table below.
I want to Select column A1:A9 and C1:F9. The Selection also needs to be adaptive to different column sizes (i.e, someone adds another Feature). The macro should also work for a table of data, anywhere in the Sheet. As long as the macro originates from the ActiveCell.
How do I not only Select until the column end, but also Select excluding the "Values" column. I am trying to use End and Offset, but I am not sure the best way to do it. And once again, I want to use it on a table that is anywhere in the sheet and then create a Stacked Column Chart from it.
Thanks for your help!
Try this code please. The idea is that you iteratively Union various ranges of the data based on the condition that the header is not 'Values'.
The working assumption is that the CurrentRegion of the ActiveCell will select your table data. Where the definition of CurrentRegion is 'The current region is a range bounded by any combination of blank rows and blank columns. ' - MSDN link
Then the code will append the first column to an output range. After that, the outer columns will only be appended to the output range if the header is not 'Values'.
Dim rngData As Range
Dim intCounter As Integer
Dim rngToSelect As Range
Set rngData = ActiveCell.CurrentRegion
Set rngToSelect = Range(rngData.Cells(1, 1), rngData.Cells(rngData.Rows.Count, 1))
For intCounter = 1 To rngData.Columns.Count
If rngData.Cells(1, intCounter).Value <> "Values" Then
Set rngToSelect = Union(rngToSelect, Range(rngData.Cells(1, intCounter), rngData.Cells(rngData.Rows.Count, intCounter)))
End If
Next intCounter
rngToSelect.Select
I have an excel table with one header row and one data body row. I want to count the data body rows. When I'm trying to check how many rows my table has with
Set myWorkSheet= ActiveWorkbook.Worksheets("Sheet1")
Set myTable= myWorkSheet.ListObjects("Table1")
countrows = myTable.ListRows.Count
countrows contains 0. If the has 2 or more rows, it gives the right row number. Why does it say 0 for one row ans is it the best way to count the rows or are there better ones?
EDIT:
Found out whats causing the problem. I use this lines to clear the table before i fill it again:
If tblChart.ListRows.Count >= 1 Then
myTable.DataBodyRange.Delete
End If
After that operation the table looks like I described it. Without it and modifying the table to look like I described the table it worked. But why is it causing this problem?
The ListObject property you are looking for is the .DataBodyRange.
Dim myWorkSheet As Worksheet, myTable As ListObject, countRows As Long
Set myWorkSheet = ActiveWorkbook.Worksheets("Sheet1")
Set myTable = myWorkSheet.ListObjects("Table1")
countRows = myTable.DataBodyRange.Rows.Count
Debug.Print countRows
A comprehensive list of the ListObject properties is available at: ListObject Interface.
Responding late in-case Google search brings user here:
I found this problem as well. DataBodyRange object does not exist unless there are two rows of data. Not just two empty rows, but two rows of data in at least one column.
What I have found to work reliably is to test if DataBodyRange exists:
If Not TableListObject.DataBodyRange is Nothing Then
Debug.Print "Data Rows Count=", TableListObject.DataBodyRange.Rows.Count
Else
Debug.Print "No Data in Table detected"
End if