Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

typeName question

Status
Not open for further replies.

eHanSolo

Technical User
May 24, 2004
260
0
0
GB
HI guys,

Can anyone explain to me the following:

public function AvgLoss(returns as variant, arg2) as double
'supposed to check arg1
If (TypeName(Returns) <> "Range") Then
AvgLoss = "#N\A Arg1"
Exit Function
End If

'''''''''''''
The only thing i can't seem to comprehend is the typename bit. Can anyone rephrase the above lines as pseudo code for my tiny brain plz?

My current interpretation is: If the 'Returns' argument(type??) IS NOT equal to 'Range' then AvgLoss will equal to: "#N\A Arg1"


Please help.

Thank you!!!

E
 
Hopefully this code should answer your question :-
Code:
Sub test()
    Dim MyVariable As Variant
    '------------------------
    Set MyVariable = ActiveSheet.Range("A1")
    MsgBox (TypeName(MyVariable))
    '---------------------------
    MyVariable = "A string"
    MsgBox (TypeName(MyVariable))
    '---------------------------
End Sub

In the VB Editor put the cursor into Typename and press F1 key - or see Help in the usual way.


Regards
BrianB
Use CupOfCoffee to speed up all windows applications.
It is easy until you know how.
================================
 
i've done that already...

thanks all the same though Brain.

E
 
Returns is an object container - can be any variable type as it is declared as variant
That function is testing whether the variable you have passed as "Returns" in the argument list is a Range object or not

If it is a range object, the function concludes - if it is not, the #N/A Arg1 is returned

TypeName returns the data type of a variable ie String, Integer, Double etc BUT it shouldn't return "Range" ever - that is not one of the constants associated with TypeName so I can't see how it would work properly...

Rgds, Geoff

"Having been erased. the document thjat you are seeking. Must now be retyped"

Please read FAQ222-2244 before you ask a question
 
shouldn't return "Range" ever

If you copy/paste & run the code I posted earlier you will see that this is not the case.


Regards
BrianB
Use CupOfCoffee to speed up all windows applications.
It is easy until you know how.
================================
 
Hi E,

Typename certainly can return "Range", but the posted code looks like nonsense. If the first argument isn't a range it will give a Type Mismatch trying to assign a String literal to a Double.

Incidentally, your interpretation of the code is exactly right as far as it goes (with the caveat as above that it will actually error out)



Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
Code:
public function AvgLoss(returns as variant, arg2) as double
'supposed to check arg1
If (TypeName(Returns) <> "Range") Then
        AvgLoss = "#N\A Arg1"
        Exit Function
End If

'''''''''''''
Some observations:
1. Both arguments "returns" and "arg2" are Variants (arg2 is variant by default)
2. Since AvgLoss is defined as a double, the attempt to put a string into it fails and the function returns #VALUE!

To answer your question:
<pseudo code>
This is a function named "AvgLoss" with two arguments and a double result value.
If the first argument is not a range, attempt to return the value "#N\A Arg1" and then exit the function.
Otherwise continue executing code as defined in the remainder of the function.
</pseudo code>

If you start typing inside a function or sub: "Dim z As "
you should see a list of all of the possible object types that can be declared. Most (if not all) can be passed as a Variant. (That includes "Range")
 
wow... thanks guys... didn't think my post would ignite such a response!!! THANK YOU for all your input!!!!

E
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top