ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Variable Data Type (https://www.excelbanter.com/excel-programming/444995-variable-data-type.html)

kittronald

Variable Data Type
 
How can you tell the data type of a variable ?

The defined name "Test" has a Refers to: field of =SUM(A1,B1).

1) In the example below, what would the data type for "v" be ?

v = Application.Evaluate(ActiveWorkbook.Names("Test"))

2) For a ComboBox containing text values, what would the data type for
"x" be for the currently selected value ?




- Ronald K.



Ron Rosenfeld[_2_]

Variable Data Type
 
On Wed, 28 Sep 2011 20:36:47 -0400, "kittronald" wrote:

How can you tell the data type of a variable ?

The defined name "Test" has a Refers to: field of =SUM(A1,B1).

1) In the example below, what would the data type for "v" be ?

v = Application.Evaluate(ActiveWorkbook.Names("Test"))

2) For a ComboBox containing text values, what would the data type for
"x" be for the currently selected value ?




- Ronald K.


The data type of a variable is determined by your type declaration. If you don't specifically declare the data type (a poor practice, in my opinion), it will be a variant data type.

Martin Brown

Variable Data Type
 
On 29/09/2011 01:36, kittronald wrote:
How can you tell the data type of a variable ?

The defined name "Test" has a Refers to: field of =SUM(A1,B1).

1) In the example below, what would the data type for "v" be ?

v = Application.Evaluate(ActiveWorkbook.Names("Test"))


Depends on whether =SUM(A1,B1) gives a valid answer or not.

Success will be a Double or failure will be an Error
(there might be other possibilities)

Simple clip to test

Sub test()
x = 1
y = "hello"
Z = Application.Evaluate("=SUM(A1..B2)")

Debug.Print x, TypeName(x)
Debug.Print y, TypeName(y)
Debug.Print Z, TypeName(Z)
End Sub


2) For a ComboBox containing text values, what would the data type for
"x" be for the currently selected value ?


I think either a Long internally or a Double in the linked cell
depending on exactly how you ask the question. Do the experiment to
check it to be sure.

Regards,
Martin Brown

Clif McIrvin[_3_]

Variable Data Type
 
"Martin Brown" wrote in message
...
On 29/09/2011 01:36, kittronald wrote:
How can you tell the data type of a variable ?

The defined name "Test" has a Refers to: field of =SUM(A1,B1).

1) In the example below, what would the data type for "v" be ?

v = Application.Evaluate(ActiveWorkbook.Names("Test"))


Depends on whether =SUM(A1,B1) gives a valid answer or not.

Success will be a Double or failure will be an Error
(there might be other possibilities)

Simple clip to test

Sub test()
x = 1
y = "hello"
Z = Application.Evaluate("=SUM(A1..B2)")

Debug.Print x, TypeName(x)
Debug.Print y, TypeName(y)
Debug.Print Z, TypeName(Z)
End Sub


2) For a ComboBox containing text values, what would the data
type for
"x" be for the currently selected value ?


I think either a Long internally or a Double in the linked cell
depending on exactly how you ask the question. Do the experiment to
check it to be sure.

Regards,
Martin Brown


I have on occasion used typename() against a range.value to test for
error conditions -- that is one advantage of using the variant type
instead of specific typing. If memory serves, the iserror() function can
be useful when working with variants and formula results.

--
Clif McIrvin

(clare reads his mail with moe, nomail feeds the bit bucket :-)



kittronald

Variable Data Type
 
Clif,

Thanks for the help.



- Ronald K.



kittronald

Variable Data Type
 
Martin,

Using Debug.Print helped a lot.

The problem I was running into was that the Refers to: field had an IF
statement (i.e., =IF(SUM(A1,B1)3,SUM(A1,B1),"Error").

So the result could be a number or a text value.

Is "As Variant" the appropriate data type for this type of situation ?

Thanks again for the help.



- Ronald K.




All times are GMT +1. The time now is 09:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com