Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
qlikplaut
Partner - Contributor III
Partner - Contributor III

Sort via Macro question

Hello,

I am using a Macro to allow a button to sort a table using the first dimension.

Sub SortChart 

Set ch = ActiveDocument.GetSheetObject("CH01") 

ch.SortBy 0 '' this line specifies the expression to sort by 

  

Set p = ch.GetProperties 

Set dims = p.Dimensions 

'' Dimensions start by 0 

'' 0 = Disable sort 

'' 1 = Sort ascending 

'' -1 = Sort descending 

i = dims.Item(0).SortCriteria.SortByNumeric 

If i = 1 Then 

  dims.Item(0).SortCriteria.SortByNumeric = 1 

Else 

  dims.Item(0).SortCriteria.SortByNumeric = -1 

End If 

ch.SetProperties p 

End Sub 

When I have 2 dimensions in the table, I would like this macro to sort both of them, first sort using the first dimension and after using the second.

I can do that from Chart Properties-> Sort, but I need to do it over this macro.

Any ideas?

Thank you in advance,

Bogdan

4 Replies
Gysbert_Wassenaar

dims.Item(0) is the first dimension. If you want to sort another dimension add another if-then-else block for the second dimension using dims.Item(1). Or add them in the same block if you want to sort all the dimensions in the same order.


talk is cheap, supply exceeds demand
qlikplaut
Partner - Contributor III
Partner - Contributor III
Author

Hello,

Thank you for your response. Add another dim. to the macro as you suggested, it will sort the table but the primary sorting option will remain the second dimension. I need to sort primarily from the dim0 and secondarily from dim1. I find it easier to explain with examples:

Here is what I want to achieve:

dim0dim1
a1
a2
a3
b1
b2
b3

Here is what it does now:

dim0dim1
a1
b1
a2
b2
a3
b3

Bogdan

marcus_sommer

You could change the order in which you applied this sorting or separate them in multiple executions - but perhaps you need only another SortCriteria - see this example from APIGuide.qvw:

rem set load order sort for all fields in chart Mychart

set chart=ActiveDocument.GetSheetObject("CH01")

set Prop = chart.GetProperties

set vars = Prop.Dimensions

for i = 0 to vars.Count-1

    vars.Item(i).SortCriteria.SortByAscii = 0

    vars.Item(i).SortCriteria.SortByLoadOrder = 1

next

chart.SetProperties Prop

- Marcus

qlikplaut
Partner - Contributor III
Partner - Contributor III
Author

Thank you for your reply Marcus.

Might be sloppy, but I managed to make it work, here is what I used:

Sub SortChart

Set ch = ActiveDocument.GetSheetObject("CH01") 

ch.SortBy 1 '' this line specifies the expression to sort by 

Set p = ch.GetProperties 

Set dims = p.Dimensions 

'' Dimensions start by 0 

'' 0 = Disable sort 

'' 1 = Sort ascending 

'' -1 = Sort descending 

i = dims.Item(1).SortCriteria.SortByNumeric 

  If i = 1 Then 

  dims.Item(1).SortCriteria.SortByNumeric = -1 

Else 

  dims.Item(1).SortCriteria.SortByNumeric = 1 

End If 

ch.SetProperties p 

Set ch = ActiveDocument.GetSheetObject("CH01") 

ch.SortBy 0 '' this line specifies the expression to sort by 

  

Set p = ch.GetProperties 

Set dims = p.Dimensions 

i = dims.Item(0).SortCriteria.SortByNumeric 

If i = 1 Then 

  dims.Item(0).SortCriteria.SortByNumeric = -1 

Else 

  dims.Item(0).SortCriteria.SortByNumeric = 1 

End If 

ch.SetProperties p

End Sub