Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Macro to select next value in Listbox

I have a listbox that can only have 1 selected value and want to create buttons that select the next (or previous) value in the listbox. In pseudo code, it should do the following:


Sub SelectNext
get [index of currently selected value]
if index < [number of rows in listbox] then
listbox.select [index + 1]
end if
End Sub


Can anyone share a macro that would accomplish this?

1 Solution

Accepted Solutions
disqr_rm
Partner - Specialist III
Partner - Specialist III

See enclosed. Hope this helps you.

View solution in original post

10 Replies
disqr_rm
Partner - Specialist III
Partner - Specialist III

See enclosed. Hope this helps you.

Not applicable
Author

Hi Rakesh,

This is exactly what I was looking for, many thanks!

Just to make sure I understand what this code does: it loops through the listbox until it finds the currently selected value, when it does it sets the "IamDone" flag so that during the next iteration of the loop it exits and selects the next value.

Changing for i=0 to val.Count-1 to for i=val.Count-1 to 0 step -1 makes the code select the previous value.


Sub SelectNext
set mySelections = ActiveDocument.fields("Id").GetSelectedValues(1)
ActiveDocument.Fields("Id").Clear
if mySelections.Count = 0 then
set val=ActiveDocument.Fields("Id").GetPossibleValues(1)
ValueToSelect = val.Item(0).Text
else
set val=ActiveDocument.Fields("Id").GetPossibleValues
IamDone = " "
for i=0 to val.Count-1
ValueToSelect = val.Item(i).Text
if IamDone = "X" then
exit for
end if

if val.Item(i).Text = mySelections.Item(0).text then
IamDone = "X"
end if
next
end if
ActiveDocument.Fields("Id").Select ValueToSelect
End Sub


disqr_rm
Partner - Specialist III
Partner - Specialist III

Yes, you got the logic correct. That's exactely what it does.

Not applicable
Author

You already have a button that does that. Click on the listbox title to activate it and then click the down arrow on your keyboard. The selection will move to the next value.

johnw
Champion III
Champion III

Much simpler macro to do the same thing:

sub selectNext
ActiveDocument.getField("LIB_CENTRE").select ActiveDocument.Evaluate("minstring({1}if(LIB_CENTRE>'$(=LIB_CENTRE)',LIB_CENTRE))")
end sub

Another couple approaches are to hide a slider bar behind the list box with only the arrow icons showing, or to use a semantic load and select previous/next that way. I can post examples of all three if desired. But I think just using the keyboard is the best approach.

Not applicable
Author

Hi Rakesh

The macro works perfectly; thank you very much for posting it!

What changes are required to make it into a 'Select Previous' instead of a 'Select Next' macro?

Your help is much appreciated.

Thanks,

Patrik

disqr_rm
Partner - Specialist III
Partner - Specialist III

Hi Patrik

According to ph1979

"Changing for i=0 to val.Count-1 to for i=val.Count-1 to 0 step -1 makes the code select the previous value."

Did you try that?

Not applicable
Author

That works perfect as well, but when I use that macro the 'Next' macro does not work anymore. It seems like I am only able to have one (1) macro per sheet even if the name of the second one a different name??

To be able to have one 'next' and one 'previous' button on the same sheet it seems like I need to use one (1) macro for both. I guess this can be done by writing the macro twice user separate IF statements using or i=0 to val.Count-1 and for i=val.Count-1 to 0 step -1.

How would I write the IF statement though:)?

Thank you very much!!

-P

richardpayne
Creator
Creator

I have used the macro Rakesh supplied, which seems to traverse the list of values for my list box pretty well, but it does not traverse them in order.

This is also true when attempting to use a slider. I have values for an "Item Number" loaded in alphanumeric order, but when I attempt to use this macro or a slider, few items will be in order, but it mostly jumps around the list.

Is there anything I should be checking for?

An acceptable substitution for this solution would be a static (selected value doesn't jump to top) list box that removes excluded values while leaving a contiuous list of the possible field values which are based on other selections, which I have not been able to accomplish. Selecting this box and hitting arrow up/down would be acceptable, but it needs to show the entire list of possible items. I can arrow up/down now, which traverses the list in order, but does not display the full available list or items.