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: 
Anonymous
Not applicable

Use of selected value in macro

I have a macro to export some data from QlikView to Excel. In QlikView I have a multibox with a table column called "Cycle" which can take the following values:"Cycle 1","Cycle 2","Cycle 3" or none of these values. How can I change what my macro does, depending on whether I have selected a Cycle value or not, and the value of the said Cycle value?

1 Solution

Accepted Solutions
tamilarasu
Champion
Champion

Hi Mihai,

Here is the possible scenario checks code. You can adapt this as per your need. Please let me know if you have any questions.

Sub Test

If ActiveDocument.Fields("Year").GetSelectedValues.Count = 0 Then

Msgbox "Nothing is selected"

Else

if ActiveDocument.Fields("Year").GetSelectedValues.Count >1 Then

Msgbox "Multiple Values Selected"

Else

SET Field = Activedocument.Fields("Year").GetSelectedValues

  If Field(i).text ="Cycle1" Then
    [Do something]
  Else
      If Field(i).text ="Cycle2" Then
        [Do something else]
      Else
        'If Field(i).text ="Cycle3" Then    ' You can uncomment this line if you want.
            [Do something Different]
        'End If
      End if
  End If


End If

End If

End Sub

View solution in original post

19 Replies
danielgarciamej
Contributor II
Contributor II

Hello Mihai


You can get the selected value of the field with the following function:

     selectedVaue = ActiveDocument.Fields("Name Of Your Field").GetSelectedValues.Count

On the other hand, you can control what the macro does if you put different IF sentences inside your macro, like this:

     If selectedVaue = 'Sample Value A of Cycle' Then

   

          [Put the code here]

    

     End If

 

     If selectedVaue = 'Sample Value B of Cycle' Then

   

          [Put the code here]

    

     End If

I hope I've helped.

Best regards

Daniel

zhadrakas
Specialist II
Specialist II

have a look at the QlikView api guide. There are examples for nearly everything you can do with macros:

API GUIDE

goto Automation exmaples tab and search for:

- .getselectedValues

- .Select

*mousover the selected value

Anonymous
Not applicable
Author

I'm using the personal Edition and I got an "License is embedded in this document is not valid" error.

Anonymous
Not applicable
Author

Could you help me with some macro lines?

Anonymous
Not applicable
Author

ActiveDocument.Fields("Name of my field").GetSelectedValues.Count only counts whether I have selected any value of the field "Cycle" or not. Which is partially useful in my future nested IFs in macro. But I need to get the value I have selected.

I've tried using only ActiveDocument.Fields("Name of my field").GetSelectedValues, to get the value that I have selected in the field, but I keep getting an "Unknown Runtime Error".More exactly the line with the error is:

XlDoc.Sheets(1).Range("Q1").Value = cycle_value

How can I use cycle_value in a Nested IFs? Am I using it wrong? 

I'm looking for something like:
if cycle_value = "Cycle1" then

[Do something]

end if

if cycle_value = "Cycle2" then

[Do something else]

end if

if cycle_value = "Cycle 3" then

[Do something different]

end if

danielgarciamej
Contributor II
Contributor II

I think that you have to develop a macro with the following structure:

0) Create a variable like:

vValueSelected = [Cycle Field]

1) Count the total values in you table column. Use:

     totalValues = ActiveDocument.Fields("Cycle").GetSelectedValues.Count 


2) Obtain all values and save into variable


     Set allValues = ActiveDocument.Fields("Cycle").GetSelectedValues

3) A loop from i=0 to totalValues

4) (Inside the loop) Obtain the value of field Cycle of each iteration

     valueCycleIteration = allValues.Item(i).Text

    

     If valueCycleIteration = 'Value 1' Then

    

          [Put some code]

     End If

     If valueCycleIteration = 'Value 2' Then

    

          [Put some code]

     End If

     ...

tamilarasu
Champion
Champion

Hi Mihai,

Here is the possible scenario checks code. You can adapt this as per your need. Please let me know if you have any questions.

Sub Test

If ActiveDocument.Fields("Year").GetSelectedValues.Count = 0 Then

Msgbox "Nothing is selected"

Else

if ActiveDocument.Fields("Year").GetSelectedValues.Count >1 Then

Msgbox "Multiple Values Selected"

Else

SET Field = Activedocument.Fields("Year").GetSelectedValues

  If Field(i).text ="Cycle1" Then
    [Do something]
  Else
      If Field(i).text ="Cycle2" Then
        [Do something else]
      Else
        'If Field(i).text ="Cycle3" Then    ' You can uncomment this line if you want.
            [Do something Different]
        'End If
      End if
  End If


End If

End If

End Sub

Anonymous
Not applicable
Author

Thanks, didn't knew that GetSelectedValues method returns an array.

Anonymous
Not applicable
Author

Here's how I'm using your scenario:

if cycle_flag(i).text = "Cycle1" then

Start_value = ActiveDocument.GetVariable("Cycle1_start")

End_value = ActiveDocument.GetVariable("Cycle1_stop")

else

if cycle_flag(i).text = "Cycle2" then

Start_value = ActiveDocument.GetVariable("Cycle2_start")

End_value = ActiveDocument.GetVariable("Cycle2_stop")

else

if cycle_flag(i).text = "Cycle3" then

Start_value = ActiveDocument.GetVariable("Cycle3_start")

End_value = ActiveDocument.GetVariable("Cycle3_stop")

end if

end if

end if

But I get a "Type mismatch: 'cycle_flag'" error