MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Changing data type

  Asked By: Bertha    Date: Nov 25    Category: MS Office    Views: 1699

Can we initially define a variable (say b) as variant and later in the
code change b to some other data type like long or double.



5 Answers Found

Answer #1    Answered By: Faith Hughes     Answered On: Nov 25

No, you can't change  a variable's type  after it is defined. However, you can
define it as something else in a different part of your project (although that
would be a BAD idea).

If it's defined as a variant, then you can put anything you like into it.

Why do you want to change its definition? (More to the point, why are you
defining it as variant in the first place?)

Answer #2    Answered By: Dinh Tran     Answered On: Nov 25

By defining as variant, I can copy a range of cells data  (in integer
or long  data type) from excel sheet in the variant variable  by single
command (saves computational time).

Later, I wanted to use this variable defined as variant in data type
integer as all my data is integer. Reason:
1. I need to pass this variant variable data to a subroutine which
accepts parameters of type  long.
2. It can save data storage space.

Answer #3    Answered By: Ann Evans     Answered On: Nov 25

I don't understand what you are trying to do.

If you know your data  is going to be long  or integer, then I guess you should
declare it as long (since if you declare it as integer and it is outside range
-32,768 to 32,767 then you will get an error message).

Now, if you want to pass this data as a parameter to a function then, pass it
as it is
You stated that your function accepts data of type  long...

long requires 4 bytes of memory and variant 16 bytes....

Answer #4    Answered By: Dan Romero     Answered On: Nov 25

Then perhaps all you need are the type  conversion functions:
Cint = Converts data  to Integer
CStr = Converts data to String
CLng = Converts data to Double

Answer #5    Answered By: Clint Garcia     Answered On: Nov 25

If I get you right, the data  you are extracting as a
range is all integers. You type  the variable  as
variant to create an array (and extract the data in
one fell swoop). Your problem then is that even though
the data is integer, the typing of variant is not accepted
by your other subroutine.

One workaround might be to create another variable (an array).
Dim it as aaNew() as Integer. Then, redim it once you know
the dimensions (unless they stay the same, then just dim it
as aaNew(rr,cc) as Integer. Then, copy, in a loop, data
from aaVariant.

The time consuming part is extracting data from Excel cells in
a loop. I too have experienced this -- dumping the mass range
is much much faster. However, I think that you'll find that
going from aaVariant to aaNew in a loop (array to array) will
happen in a nanosecond, so this extra step should not impact
your speed, and your other subroutine should like the data type.

Didn't find what you were looking for? Find more on Changing data type Or get search suggestion and latest updates.