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

Set default field selection using variable - possible??

Hi,

I want to write a macro which will do two things:

1. Clear all selections (I know how to do).

2. Set a field selection based on the content of a variable.

    Example: I want to do something like this

    set = vYearSel = '2016'      -> in load script

    ActiveDocument.Fields("year").Select $(vYearSel)     -> in macro.

When I ran it, the macro failed to parse.   If I hardcode the value in the macro, it will work which isn't what  I want.

Anyone know how I can accomplish this?

Thanks!!

1 Solution

Accepted Solutions
m_woolf
Master II
Master II

As I have said many times in previous posts, this is easier with actions instead of macros. Add to that, macros generally work only with Internet Explorer and the QV Plugin (there are some exceptions.

In the OnLeaveSheet trigger, you can add an Action that saves your Year selection into a variable.

In the OnActivateSheet trigger, you can add an action that selects the Year stored in the variable. No VB code is necessary.

If you found any of this useful, please mark replies as Helpful or Correct.

View solution in original post

20 Replies
m_woolf
Master II
Master II

This can be don without using a macro, but just using actions.

If you insist on using a macro, you need to get the value of the variable into VB something like this:

set v = ActiveDocument.Variables("vYearSel")

strYearSel = v.GetContent.String

Then use strYearSel in your Select statement.

ActiveDocument.Fields("year").Select strYearSel

Anonymous
Not applicable
Author

When I surround the $ with quotes like

ActiveDocument.Fields("year").Select "$(vYearSel) "


It parsed successfully but I don't see my selection being made.  Not sure if this code actually ran or if it ran w/

errors.  I did not see any errors on the screen.  Surrounding with double quotes probably isn't going to work.  It

may just treat it as a literal string.  But I just gave it a try ...

Anonymous
Not applicable
Author

MW,

I just tried your code.  When I ran it, it opened the macro window which means something is wrong.  It doesn't like this

line

strYearSel = v.GetContent.String


I also replaced your variable with mine (vYearSel) which is in my load script, it didn't like it either.


Anything I'm missing?


Anonymous
Not applicable
Author

I also tried this and it didn't like it as well.

msgbox(v.GetContent.String)

m_woolf
Master II
Master II

This is really a lot simpler to do with actions.

Please post your code with my changes.

Anonymous
Not applicable
Author

Here's the SUB.  I replaced your var with mine - vYearSel.  The last line that I tested/debugged is the msgbox().

sub SetYearSel

  set v = ActiveDocument.Variables("vYearSel")

  'strYearSel = v.GetContent.String()

  'vYearSel = v.GetContent.String

  msgbox(v.GetContent.String)

  'ActiveDocument.Fields("year").Select strYearSel

end sub

m_woolf
Master II
Master II

Try:

sub SetYearSel

     set v = ActiveDocument.Variables("vYearSel")

     strYearSel = v.GetContent.String

     ActiveDocument.Fields("year").Select strYearSel

end sub

Anonymous
Not applicable
Author

Hi,

Very strange ...  This is what I did:

1. Copied your code exactly as you posted it.

2. Clicked the button which calls SetYearSel

3. Again, it opened the macro window.  Didn't work

4. Created a brand new document with only the Year listbox, your code and a button.

    Clicked the button and it worked.  So I know your code works.

5. Re-opened the problem document and removed everything except what's needed to run this (just like in

    Step 4) and it failed (macro window opened).  I checked the spelling of the variable in load script and the macro

    and there was nothing wrong.

6. Now what??  I decided to copy the variable (vYearSel) from the macro and replaced in my load script.

    see screenshot.  Clicked the button and unbelievably it WORKED !!!.

qv06.jpg

So not sure what happened there?  I've run into this issue in QV before when a line of code (or a variable) didn't work even though I checked over and over again, I deleted it and re-typed and it worked.  In this case, I just did a <CRTL+C> and <CTRL+V>.

Is there a bug in QV? 

Thanks for your help!

Anonymous
Not applicable
Author

Slightly different question but related.

When I click on a different tab/sheet meaning leaving the current sheet, what's the API to save the current

Year selection into a variable before activating the new sheet?  I know there's an event "OnLeave".  But what's the code to actually save it?  

Thanks!