Run Macro after OLAP Query - vba

This is my Workbook:
Sheets layout:
Source: Pivot Table with Analysis Services connection
Distributors: Stuff to be sorted connected with the Pivot
Output: Chart based on sorted data of Distributors + Slicers connected to Pivot
What I need to do is: Launch Sorting Macro after each OLAP Query (= each time I use the slicers).
Sorting Code
Sub Sorting()
'This line finds the last occupied row in column A
'And you can use that LR variable in all the following Range Statements.
LR = Cells(Rows.Count, "C").End(xlUp).Row
ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Add Key:=Range("C4:C102" & LR) _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Distributors").Sort
.SetRange Range("A3:C102" & LR)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Distributors").Sort.SortFields.Add Key:=Range("D4:D102") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Distributors").Sort
.SetRange Range("D3:F102")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Solved. Launch Macro after each Pivot Update
Alt+F11, Right click on Source (Sheet with PivotTable)
View Code
Insert the Event trigger
Event Trigger
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
'your_macro_here
End Sub
Keep in mind that you can't enter reference to a Module. Insert directly your macro.

Related

Why isn't my VBA sorting my column data correctly?

I am trying to delete the first row of the Excel sheet and sort a specific column using its name "CUST_RELPO". I am using the column header name since the name may change.
Sorting and copying the column from the second row since I do need to copy the column header.
Sub ClearFirstRow()
'
' ClearFirstRow Macro
'
'
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Cells.Select
Dim rngcustrelpo As Range
xindex = Application.ActiveCell.Column
Set rngcustrelpo = ActiveSheet.UsedRange.Find("CUST_RELPO")
If rngcustrelpo Is Nothing Then
MsgBox "CUST_RELPO column was not found."
Exit Sub
End If
'Cells.Select
Range(rngcustrelpo, rngcustrelpo.End(xlDown)).Select
ActiveWorkbook.Worksheets("BACKORDER").Sort.SortFields.Add Key:=ActiveSheet.UsedRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.SetRange ActiveSheet.UsedRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Set rngcustrelpo1 = rngcustrelpo.Offset(1, 0)
Range(rngcustrelpo1, rngcustrelpo1.End(xlDown)).Select
Selection.Copy
End Sub
However, it is not sorting the data like I am expecting. I am not sure what I am missing here.
Key:=ActiveSheet.UsedRange is a complete misunderstanding of the sort method. (Usedrange covers the whole used area on the sheet - often "empty" cells, too.) The same applies to .SetRange ActiveSheet.UsedRange. It is not bad just needless. SetRange is needed when you want to limit the area to be sorted. If you want to sort on only one key (column), then change this
ActiveWorkbook.Worksheets("BACKORDER").Sort.SortFields.Add Key:=ActiveSheet.UsedRange, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.SetRange ActiveSheet.UsedRange
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
to this:
With ActiveWorkbook.Worksheets("BACKORDER").Sort
.Key rngcustrelpo
.Header = xlYes
.MatchCase = False
.Order:=xlAscending
.Orientation = xlTopToBottom
.SortOn:=xlSortOnValues
.DataOption:= xlSortTextAsNumbers
.SortMethod = xlPinYin
.Apply
End With
And you can find more info here: Excel SortFields add then sort and here: Most efficient way to sort and sort syntax VBA

Looping macro to work on all columns individually without writing long list of repeated transitions

I am trying to loop this macro (below) to go through all me data columns but need some help creating a loop or adjusting the current code to work for all columns.
Sub Trial_5()
'
ActiveCell.Offset(0, -7).Columns("A:A").EntireColumn.Select
ActiveWorkbook.Worksheets("Sheet6").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet6").Sort.SortFields.Add Key:=ActiveCell, _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet6").Sort
.SetRange ActiveCell.Range("A1:A16395")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
`enter code here`.SortMethod = xlPinYin
.Apply
End With
End Sub
do I adjust the .columns or the activecell.offset to total range??
adjusted script
Sub eachcolumndesending()
'
' eachcolumndesending Macro
' descending
'
'
Columns("A:A").Select
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet5").Sort
.SetRange Range("A2:A32")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Columns("B:B").Select
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet5").Sort.SortFields.Add Key:=Range("B1"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet5").Sort
.SetRange Range("B1:B33")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I suggest you move away from the recorded code to what VBA actually requires for a single column sort.
Sub sortAllColumns()
Dim c As Long
On Error Resume Next '<~~ may be necessary if a column breaks the sort
With Worksheets("Sheet5")
For c = .UsedRange.Columns.Count To 1 Step -1
With .Columns(c)
.Cells.Sort Key1:=.Columns(1), Order1:=xlDescending, _
Orientation:=xlTopToBottom, Header:=xlGuess
End With
Next c
End With
End Sub
Btw, you probably shouldn't have to xlGuess at the existence of a header. Either one is there (xlYes) or not (xlNo) but you know your data better than I.

excel Vba error 400 when I added sorting to the end of my macro

I Have a relatively long sub that adds data to another sheet in a bunch of columns based on where its coming from. this part of the code works perfectly, however I wanted to sort all the rows in the table up to the last row that was added. the sorting code that I added on to the end works if I hard code what cells to include in the range, but my range will grow each time the sub is run so I tried to make the range include the variable I named for the next empty row (1MaxRows).
When I do this I get an error that says "400", in the past when I have gotten this error it is because i referenced the sheet or workbook wrong, but this time I didn't change any sheet references. The section of my code that gives me this error is as follows:
Columns("A:Q").Select
ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Add Key:=range("A2:A & lMaxRows" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Add Key:=range("B2:B & lMaxRows" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Raw Data").Sort
.SetRange range("A1:Q & lMaxRows")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I have tried changing the range codes in a handful of ways but I always either get an overflow error or a 400 error.
Thank you for the help.
You should qualify your Range calls with the correct worksheet, and you also have quotes in the wrong place in your addresses. For example:
Dim ws As Worksheet
Set ws = ActiveWorkbook.Worksheets("Raw Data")
With ws.Sort.SortFields
.Clear
.Add Key:=ws.Range("A2:A" & lMaxRows), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Add Key:=ws.Range("B2:B" & lMaxRows), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
End With
With ws.Sort
.SetRange ws.Range("A1:Q" & lMaxRows)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I've assumed you have properly declared and assigned a value to lMaxRows (and note that it is LMAXROWS and not 1MAXROWS with a number at the start.

Select ActiveCell in another sheet

I am working on a project that will sort all the cells in another worksheet based on criteria. I need to activate a cell in another worksheet to make it work. I tried finding a solution but no luck.
I recorded a macro from my workbook and tweaked it a bit so it would fit my need. When I run the code, Runtime Error 1004 appears.
I need a code to replace
SetRange ActiveCell.Offset(-1, 0).Range("A1:AF30436")
Any suggestion will be appreciated.
Here's the code:
Sheets("Source").Select
ActiveWorkbook.Worksheets("Source").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Source").Sort.SortFields.Add Key:=ActiveCell.Range _
("A1:A30435"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Source").Sort
.SetRange ActiveCell.Offset(-1, 0).Range("A1:AF30436")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
when you record macro, you click on the cell in the second sheet and start the sort routine. Macro correctly recorded this.
however when you are doing this in VBA, the "active cell" could be anything and in most cases you dont need this as long as you know the datarange that you need to work with
dim ws as WorkSheet
set ws = Sheets("Source")
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=ws.Range _
("A1:A30435"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ws.Sort
.SetRange Range("A1:AF30436") ' Change here to itended dataset
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

VBA to sort table and ignore total row

I have the range Table3 as shown below:
The rows are not fixed and could increase or decrease, I have thus created it as a table Table3 to accommodate this behavior and also so I could use it in a VBA as a ListObjects.
The VBA below is meant to sort the table, however because the Totals is part of the range, the sort doesn't work as intended.
Sub sort()
ActiveWorkbook.Worksheets("Project 2013").ListObjects("Table3").sort.SortFields _
.Clear
ActiveWorkbook.Worksheets("Project 2013").ListObjects("Table3").sort.SortFields _
.Add Key:=Range("Table3[Description3]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Project 2013").ListObjects("Table3").sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Can someone please help modify the code to ignore the Totals row (i.e to include only the range below the header and above the Totals row) before applying the sort
EDIT
At the moment, this is my attempt at redefining a new range without the last row
Sub sort()
Dim resizedTable As ListObject
Set resizedTable = Sheets("Sheet1").ListObjects("Table1")
With resizedTable
.Resize .Range.Resize(.Range.Rows.Count - 1, .Range.Columns.Count)
End With
resizedTable.sort.SortFields.Clear
resizedTable.sort.SortFields _
.Add Key:=Range("resizedTable[Description]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
   
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End Sub
Any help will be appreciated.
Set a new range for your table, just one row shorter » totalRowCount - 1.
Here, x is your input range
Set x = Range(x.Cells(1, 1), x.Cells(x.Rows.Count - 1, x.Columns.Count))
or use the resize method
Sub CutOffLastLine()
With ActiveWorkbook.Worksheets("Project 2013").ListObjects("Table3")
.Resize .Range.Resize(.Range.Rows.Count - 1, .Range.Columns.Count)
End With
End Sub