Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Variables in Macros

I have a macro that will randomly select based on a hard-coded number. I am trying to implement a slider that the user can use to select the number of samples they want to return. If the user selects 15 on the slider, then I want 15 samples to be pulled.The macro works if I hard-code in the number of samples, but I am having difficulty with the slider. I have a variable, vAmountSelect that represents the number from the slider. How should I change the macro to pull the amount from the variable? I thought I could do something like this in the macro, but it isn't working:

Sub SelectRandom

ActiveDocument.Fields("Field").Clear

While ActiveDocument.Evaluate("getSelectedCount(Field)") < ActiveDocument.Variables("vAmountSelect").getcontent.string

val = ActiveDocument.Evaluate("FieldValue('Field', ceil(" & Rnd() _

& " * FieldValueCount('Field')))")

ActiveDocument.Fields("Field").ToggleSelect val

Wend

End Sub

5 Replies
marcus_sommer

Try:

sub x

dim doc, fieldSelectedCount, fieldValues, i

set doc = ActiveDocument

doc.fields("Field").clear

set fieldValues  = doc.fields("Field").getpossiblevalues

fieldSelectedCount = doc.Variables("vAmountSelect").getcontent.string

for i = 1 to fieldSelectedCount

    doc.fields("Field").toggleselect fieldValues.item(round((fieldValues.count - 1) * rnd())).text

next

end sub

- Marcus

Not applicable
Author

That doesn't seem to be working for me. Can you try updating the QVW that I attached to my original post?

giakoum
Partner - Master II
Partner - Master II

what makes you think it is not working? I added a msgbox to be sure and it is working. Executed 5 times when the vAmountSelect is 5

Anonymous
Not applicable
Author

I will suggest you create a variable

vField =GetSelectedCount(Field)   // In UI


then get the variable value in macro

vField1 =ActiveDocument.GetVariable("vField ").GetContent.String

vAmountSelect1=ActiveDocument.GetVariable("vAmountSelect").GetContent.String


Now you can use these values for comparison in your condition.


Thanks

BKC




marcus_sommer

My example worked - but rnd() could return very similar values which multiplied with the fieldcounter returned a value which is already selected and the selection from a selected field meant this value will be deselected again. Especially in very small field is these effect more noticeable.

If you want avoid such behavior you need an additionally logic within the loop which checked if these value is already selected and skiped them and increased the loop-counter.

- Marcus