Specify a specific range in pivot table - excel

This is the macro I got when i recorded for pivot table creation.
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"Sheet1!R2C1:R278C35").CreatePivotTable TableDestination:="", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
These macro creates a new worksheet for pivot table report but i want the pivot table to be created in the specified sheet say
activeworkbook.sheets(2)
I was guessing TableDestionation is the path where you have to give the sheet name for pivot table creation but I dont know how to set the path over there
and I'm also Unable to figure it out how to specify a specific range in the pivot table
SourceData:= "Sheet1!R2C1:R278C35" ' how to specify a range using variables
I have to specify these range
Sheet1.range("A2:AI" & last_row)
here it takes defualt range like these
"Sheet1!R2C1:R278C35"
I'm So confused with the RC notation , Please help me with these Thanks

You can use ConvertFormula, from application, for example
Dim StrAddress As String
StrAddress = "sheet1!" & Application.ConvertFormula(Range("A1:Z300").Address, xlA1, xlR1C1)
Debug.Print StrAddress
In sample range A1:Z300 is converted in R1C1 notation, R is a row and C a column, A1 for example is ROW 1 COLUMN 1 and Z300 is ROW 300 COLUMN 26.
Use StrAddress to create your pivot.

Related

Sort fltered table data

I have this code within a loop that sets autofilter to a dynamically generated table and shows only a few rows out of many based on the filters used. The autofilter within the loop works perfect, but what I need is to sort it alphabetically (A to Z) based on the Values that came out.
My problem is that I can't manage to find a way to select the range of shown data in column A and filter it Alphabetically. Can someone help me out here? I will add a picture of what I mean :D
table_filter
[table_select](https://i.stack.imgur.com/kW2HY.png)
table_sort
Thank you guys in advance
I tried selecting the range with last cell with a value and other selection ways, but I couldn't manage to create a generic way in order to use it within a loop.
I can't find a way to grab the visible data in column A of that table and use it as a range to sort the data from a to Z
Update: I attached the code where I would like to sort the data.
So before this code is used I have a very long table with up to 64000 entries. I filter the table based on multiple criteria within an array in order to get 5 to 7 names filtered on a specific date. which you can see here:
Sheets("5.0").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("5.0").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
After this filter is applied I would like the result to be sorted from A to Z in order to grab that sorted info as you can see in the pictures above and paste it at the destination sheet.
Sub filter_with_array_as_criteria_FQ()
Dim FQ_Range As String
Dim FQ_Date As String
Dim FQ_Output_50 As String
For rep = Range("O6").Value To Range("P6").Value
FQ_Range = Sheets("Admin").Range("J" & rep).Value
FQ_Date = Sheets("Admin").Range("M" & rep).Value
FQ_Output_50 = Sheets("Admin").Range("K" & rep).Value
FQ_Output_24 = Sheets("Admin").Range("L" & rep).Value
Dim ID_range, k As Variant
ID_range = Application.Transpose(Sheets("KW_Bericht_Report").Range(FQ_Range))
For k = LBound(ID_range) To UBound(ID_range)
ID_range(k) = CStr(ID_range(k))
Next k
Sheets("5.0").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("5.0").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
'Hier muss Tabelle sortiert werden/ This is where the filtered Data needs to be sorted
'Select values from listobject column 4 paste on kwbericht
Sheets("5.0").Select
Sheets("5.0").ListObjects(1).ListColumns(4).DataBodyRange.Select
Selection.Copy
Sheets("KW_Bericht_Report").Range(FQ_Output_50).PasteSpecial Paste:=xlPasteAll
'Select Sheet 2.4
Sheets("2.4").Range("A1:D1").AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=ID_range
Sheets("2.4").Range("A1:D1").AutoFilter Field:=3, Operator:=xlFilterValues, Criteria1:=FQ_Date
'Hier muss Tabelle sortiert werden/ This is where the filtered Data needs to be sorted
'Select values from listobject column 4 paste on kwbericht
Sheets("2.4").Select
Sheets("2.4").ListObjects(1).ListColumns(4).DataBodyRange.Select
Selection.Copy
Sheets("KW_Bericht_Report").Range(FQ_Output_24).PasteSpecial Paste:=xlPasteAll
Debug.Print (FQ_Date)
Debug.Print (FQ_Range)
Next rep
Sheets("KW_Bericht_Report").Select
End Sub

Sort Data based on one column affecting the entire table VBA

I am trying to sort a table where I am sorting based on the data entries in Column E. However, when I am using my code, (below) my rows for which the column become disorganized and mismatched. Each row needs to contain the same data entries as before they were sorted, only moving to be sorted based on the values in column E. I am trying to sort this table, with my expected outcome shown below:
Code:
With Worksheets("Fruits")
Dim FruitsLastRow As Long
Dim FruitsKeyRange As Range
FruitsLastRow = .Range("E" & .Rows.Count).End(xlUp).Row
Set DashboardRange = Range("E17:G" & FruitsLastRow)
DashboardRange.Sort key1:=Range("E17:E" & FruitsLastRow), _
order1:=xlAscending, Header:=xlNo _
End With
This is what I have
This is what I need
You need to specify just the top cell instead of all cells for key1 parameter (Range.Sort example).
DashboardRange.Sort key1:=Range("E17"), order1:=xlAscending, Header:=xlNo

vlookup in VBA using selection.currentregion in another sheet

i'm new to both macros and vba
I have a pivot table in sheet FR Pivot and a table in sheet Final. I want to pick up the pivot table data and use vlookup to fill in values in my table in the Final sheet. The issue is that the pivot table can be dynamic. So I'm trying the following:
Dim Range1 As Range
With Sheets("FR Pivot")
'Select cell - You can change as per your requirement
.Range("E4").Select
'Selects the current region
Selection.CurrentRegion.Select
Set Range1 = Selection
End With
Sheets("Final").Select
Range("J7").Select
ActiveCell.FormulaR1C1 = _
"=IFNA(VLOOKUP([#[Row Labels]],Range1,2,0),""NA"")"
I think I'm going wrong with the named range part but unable to figure out how to fix it. For the macro, I'm recording my actions and then modifying code to make it dynamic which is why there are so many selects.
Without any selections:
Dim addr
addr = Sheets("FR Pivot").Range("E4").Currentregion.Address()
Sheets("Final").Range("J7").Formula = _
"=IFNA(VLOOKUP([#[Row Labels]], 'FR Pivot'!" & addr & ",2,0),""NA"")"

Autofill Dynamic Column

Trying to Autofill Column X and Column Y with text
Where Column Z determines the table length
Starting cell for column Z is "Z3" but column "X and Y" are dynamic
Last filled cell in column "X & Y" carries the text required.
Current Last cells is "X56" and "Y56"
Current last cell in column Z is "Z89"
I can easily get to x56 or y56 using
Range("Y3").Select
Selection.End(xlDown).Select
Selection.AutoFill Destination:=Range("Y56:Y89")
Range("Y56:Y89").Select
Range("X56").Select
Selection.AutoFill Destination:=Range("X56:X89")
Range("X56:X89").Select
However the solution eludes me to remove absolute references due to the dynamic nature of the information being imported and added to the column of previous information.
I tried this code i read through my research but couldn't make it work
lastRow = Range("Y3").End(xlDown).Row
Selection.AutoFill Destination:=Range("Y3:Y" & lastRow), Type:=xlFillDefault
Any assistance would be really appreciated as this appears to be the lynch pin to completing this task
Cheers
Mick
with I am trying to stack reports generated on a 12 hourly basis.
into an accrued 24 data table. This will then be accrued into a monthly data table.
As base information is downloaded in csv format. The four reports are formatted differently so i also have to stack the four reports in two table separated and itemised by date and shift.
This then allows the use of lookups, countifs sumifs etc to populate my outputs.
The four reports are dynamic and open to the potential of having a number of blank cells throughout.
I have written code that is robust enough to achieve this short of this one issue.
As the four reports do not have time stamps i am forced to use file names (column A:A) to populate the Date and Shift ranges (column A:B) as well as (Column X:Y) but need to drag the text down to cover all rows of information
Range("Y3").Select
Selection.End(xlDown).Select
Selection.AutoFill Destination:=Range("Y56:Y89")
Range("Y56:Y89").Select
Range("X56").Select
Selection.AutoFill Destination:=Range("X56:X89")
Range("X56:X89").Select
Autofill Columns with text without absolute references to allow for dynamic column range and without known starting point on the column
Do not use xlDown to find the last row. You may want to have a look at this Finding Last Row
Is this what you are trying? I have given you two option. Take your pick.
Option Explicit
Sub SampleA()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Get last row in Col Z
lRow = .Range("Z" & .Rows.Count).End(xlUp).Row
'~~> Autofill formula in 1 go
.Range("X3:X" & lRow).Formula = .Range("X3").Formula
.Range("Y3:Y" & lRow).Formula = .Range("Y3").Formula
End With
End Sub
Sub SampleB()
Dim ws As Worksheet
Dim lRow As Long
Dim rng As Range
'~~> Change this to the relevant sheet
Set ws = Sheet1
With ws
'~~> Get last row in Col Z
lRow = .Range("Z" & .Rows.Count).End(xlUp).Row
Set rng = .Range("X3:Y3")
rng.AutoFill Destination:=.Range("X3:Y" & lRow)
End With
End Sub

Change dynamic source data for pivot table Excel Macro

I've created a macro that auto updates/changes the source data of my pivot.
The problem with my macro is the source data is not static. The number of rows in trialbalance sheet changes daily so the cell reference A1:F500 is not applicable to what I'm trying to achieve.
The number of column is fixed always (columns A to F).
If someone could lend his or her helping hand on how can I detect how to get the top up to the bottom of the report found in the trialbalance sheet? That would be great!
Sheets("mtd move").pivottables("pvtmtd").SourceData = Sheets("trialbalance").[A1:F500].CurrentRegion.Address(True, True, xlR1C1, True)
ActiveWorkbook.ShowPivotTableFieldList = False
In the comment above, #DougGlancy has a great suggestion. If you are stuck with an older version of Excel that does not support the built-in Table construct, you could use the following to identify the last row, then wrap the entire data block in a Range:
'...
Dim LastRow As Long
Dim PivotSource As Range
With Sheets("trialbalance")
LastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set PivotSource = .Range(.Cells(1, 1), .Cells(LastRow, 6)) '<~ col F = 6
End With
You now have a Range object that contains all the data on the "trialbalance" sheet, i.e. all the cells from A1 (.Cells(1, 1) in the snippet above) to the last occupied row in column F (.Cells(LastRow, 6) in the above code).

Resources