Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Random selection in listbox

Hi Qliks!

I need to make a button that will select a single random value from a listbox. I am thinking that to make it easier the listbox could contain a linear range of numbers from 0 -> n.

I tried to write a macro like

Sub test

x=4

ActiveDocument.Fields("F1").Select x

End Sub

Now - I just need to figure out how to generate the variable x based on the total number of rows from the list box since the row number will grow in the model.

Your help is highly appreciated

Lars Madsen

17 Replies
Not applicable
Author

Hi,

I am using 9.00.7440.8 SR3

Lars

Not applicable
Author

hi lars,

probably check with your document properties security settings

thanks

Not applicable
Author

hi lars,

i think you have your "current local security" set as " dont run at all " in the edit module for macros.

pls set it to as only safe mode.

i hope that it should work now.pls check the attached application

thanks

Not applicable
Author

Based on the feedback I trust that Rob's solution does the trick!

Unfortunately it just doesn't work on my installation. I even tried another installation on QlikView with the same negative result. My security is set as proposed, but when I hid the button the selection is cleared completely from the listbox.

I can see though that the "back/forward" changes as if selections are in fact made with the macro - but no effect in the list box [:'(]

Thanks anyway

Lars

Not applicable
Author

Hi

I did'nt get Rob's function to work first... But then I realized that the number returned from the Rnd() funktion contained a , and QV expected a . inorder to evaluate the function...
So I could be due to system settings...

I did a simple replace and got Rob's script to work...

BR
Hans

Not applicable
Author

Hi there,

Does anyone have an idea how i can use this code and randomly select for example 100 random articles?

BR Daniel

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

You could wrap it in a While loop that runs until you get the count you want. Be careful 🙂

-Rob

Sub SelectRandom
ActiveDocument.Fields("F1").Clear
While ActiveDocument.Evaluate("getSelectedCount(F1)") < 100
val = ActiveDocument.Evaluate("FieldValue('F1', ceil(" & Rnd() _
& " * FieldValueCount('F1')))")
ActiveDocument.Fields("F1").ToggleSelect val
Wend
End Sub

llauses243
Creator III
Creator III

Hi Lars,

This is my offer, no depending on version

Pls to see image attached

Good luck, Luis

Very thx Rob.