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

Macro not running after function call

Hi,

I am trying to run the following code -

Function applySel(selDim,selVal)

  ActiveDocument.Fields(selDim).Clear

  msgbox("cleared")

  ActiveDocument.Fields(selDim).Select selVal

  msgbox("selected")

end Function

sub Apple

set s1 = applySel("fruits","apple")

msgbox("apple selected")

end sub


The msgbox is not getting executed, though I see that the selection was successfully made. Please help.

1 Solution

Accepted Solutions
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I assume you mean the "msgbox("apple selected")" is not getting executed.

A function should return something. At a minimum, the end of your applySel function should do something like:

applySel = ""


You function call "set s1=..." is expecting an object return, that's incorrect based on your Function applySel coding. I expect your macro is terminating with an "object expected" error. Since you are not returing an object your call should be:

s1 = applySel("fruits","apple")


without the set. Since you are not returning anything, you may want to consider making applySel a SUB instead.


-Rob

http://robwunderlich.com



View solution in original post

4 Replies
vikasmahajan

Please check setting

Vikas

Hope this resolve your issue.
If the issue is solved please mark the answer with Accept as Solution & like it.
If you want to go quickly, go alone. If you want to go far, go together.
rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

I assume you mean the "msgbox("apple selected")" is not getting executed.

A function should return something. At a minimum, the end of your applySel function should do something like:

applySel = ""


You function call "set s1=..." is expecting an object return, that's incorrect based on your Function applySel coding. I expect your macro is terminating with an "object expected" error. Since you are not returing an object your call should be:

s1 = applySel("fruits","apple")


without the set. Since you are not returning anything, you may want to consider making applySel a SUB instead.


-Rob

http://robwunderlich.com



Not applicable
Author

Thanks Rob. Removing 'set' worked - the details were informative.

And yes, I was talking about the "apples selected" message (will edit the other msgbox out).

I have a few other questions -

1. How do I call a sub in another sub?

2. Using

applySel("fruits","apple")

instead of

s1 = applySel("fruits","apple") throws up error - 'Cannot use parentheses when calling a Sub'

whereas when I call another function clearSel() it works fine without assigning it to any variable.

Function clearSel()

  ActiveDocument.ClearAll true

end Function

sub

clearSel()

end sub

jagan
Luminary Alumni
Luminary Alumni

Hi,

clearSel() is not receiving any arguments so you dont need to assign any variable, but applySel is having parameters so you have assign a value to applySel variable.


Syntax of function:


[Public [Default] | Private] Function name [(arglist)]

   [statements]

   [name = expression]

   [Exit Function]

   [statements]

   [name = expression]

End Function

Example:

Function ShowSum(value1, value2)

  Dim sum

  ' Determine and display the sum.

  sum = value1 + value2

  MsgBox "The sum is: " & sum

  ' Set the function's return value.

  ShowSum = sum

End Function

Calling Function:

----------------------

Dim retValue

' Call the function with and without the Call keyword.

Call ShowSum(3, 4)

ShowSum 5, 6

' Omitting the Call keyword enables access

' to the function's return value.

retValue = ShowSum(7, 😎

MsgBox "The function returned: " & retValue

Hope this helps you.

Regards,

Jagan.