0

I'm trying to convert some python scripts to VBA and haven't been able to find an equivalent to the underscore method to ignore a return variable from a function I don't want to use.

In python I would do:

[_, MyName, ret] = ETABSModel.PierLabel.GetNameList()

The equivalent statement in VBA is:

ret = ETABSModel.PierLabel.GetNameList(NumberNames, MyName())

How can I ignore the return variable NumberNames in this case?

Thanks!

0

1 Answer 1

0

You can call a VBA function exactly like a sub. If you are familiar with other programming languages, you can see a sub like a void function - a function that doesn't return anything.

Basically, there are three ways to call a routine in VBA. The first is valid only for functions:

Function foo(p1 as Variant, p2 as Variant) as Long
   (...)
   foo = val(p1) + val(p2)
End Function

Sub bar(p1 as Variant, optional p2 as Variant)
   (...)
End Sub


' (1) Call the function and do something with the return value
myVar = foo(2, 4)
myVar = foo(3, 5) + foo(4, 6)
myVar = bar(3, 5)     ' Obviously not possible, "bar" is a Sub

The second and third are valid for Subs and Functions. Both are equivalent, it's just a matter of taste. It is sometimes claimed that using Call is deprecated, however, I can't find such statement from Microsoft.

foo 2, 4   ' Call the Function
bar 2, 4   ' Call the Sub
' Or use Call
Call foo(2, 4)   ' Call the Function
Call bar(2, 4)   ' Call the Sub

Note that the following syntax is invalid. Either use Call or omit the parenthesis.

foo(2, 4)        ' Syntax error
bar(2, 4)        ' Syntax error

If you have a sub or function that takes only 1 parameter, the following syntax is valid, however, the parenthesis have a complete different meaning: It will evaluate the value of the parameter and put it on the stack. This will prevent that the value of a parameter that is called by reference is modified by the routine. You can safely assume that in 99% this is done unintentionally.

The VBA editor shows it by adding a space between the routine name and the parameter:

bar (2) 

Using the same syntax for 2 parameters makes it more obvious: bar (2), (4)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.