MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

multiplying two integer variables and assigning to a long variable

  Asked By: Lucas    Date: Mar 25    Category: MS Office    Views: 2507

I need to multiply two integer variables(a=b=1000) and put them in a
long variable. I can not afford to make a and b long because of limited
memory. vba allows a*b to be only integer(<32768) if a and b are
1. Is there a statement similar to c=long(a*b)?
2. Is it possible to put a statement in the beginning of the program to
ask vb to allow any operations to be allowed upto the data type of Left
hand side variable.

This does not run in vb
Dim a as integer, b as integer, c as long
a= 1000



4 Answers Found

Answer #1    Answered By: Brenda Fischer     Answered On: Mar 25

How about:

c = CLng(a) * CLng(b)

Answer #2    Answered By: Tasha Wheeler     Answered On: Mar 25

Conversion in VBA is actually pretty simple:
Dim A as Integer, B as Integer, C as Long


What's wrong here, though, is this method won't save memory. It will
actually increase the consumption by creating 2 new mem assignments of
Type Long and holding the output of converting A and B to Long for
whatever amount of cycles is necessary to populate C.

So the thing I have to ask is why and how are you in a position where
you've run  out of memory for simply declaring A and B as Long in the
first place?

Contrary to popular advice, the use of Variant variables  is not
something you should avoid entirely. Often, if I think I'm in memory
trouble, I go ahead and rescope everything to variant just to check
whether my memory problems are real. I don't trust the actual generated
answers, don't get me wrong. I'm just checking to see if my memory
problem still exists. If it doesn't, I'm doing something else wrong in
my code which is causing an apparent memory issue. Why?

Because any 32 bit application running in Windows believes it has 2 Gigs
of memory to work in. Windows itself builds this lie and presents it to
the application while managing the real memory in the background. Even a
system populated with only 64 megs of RAM will still tell each running
application that it has 2 gigs to work in. Consuming all 2 Gigs is an
accomplishment. Odds are against you having truly consumed that virtual

Answer #3    Answered By: Gilbert Moore     Answered On: Mar 25

Thanks for your suggestion. A is one of the entry of upto two million
row and 100 column array a of integers. Therefore, it's type  can not
be changed. Whereas this kind of multipication need to be done only
for just 500 elements of this array.

Ex: for i=1 to 500, b(i)=a(99900+i,27)*1000

Answer #4    Answered By: Adalwolfa Fischer     Answered On: Mar 25

c = CLng(a) * b..................