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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help in declaring Variables 1

Status
Not open for further replies.

uipuih

Technical User
Jan 30, 2009
6
US
I am trying get the sum of values in a column and then insert that value into a cell.
I tried the following but cannot get the MyValue to get the sum of the Range.

Dim MyRange As Range
Dim MyValue As Integer
Set MyRange = Range("E:E")
Set MyValue = Sum(MyRange)

'
End Sub
 


hi,

SUM is a spreadsheet function, not VBA...
Code:
Dim MyRange As Range
Dim MyValue As Long

'you set objects only
 Set MyRange = Range("E:E")

'other variable types just get assigned
 MyValue = Application.Sum(MyRange)

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 



BTW, the Integer data type holds from -32,768 to 32,767 and uses 2 bytes of memory. The Long data type holds from -2,147,483,648 to 2,147,483,647 and uses 4 bytes of memory.

So the use of any integer (small i) data type depends on the range of expected values.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Does the automatic conversion of Integer into Long happen in Excel as well as Word. I assume it does. In which case, it is better to simply drop using Integer, and use Long all the time.

Gerry
 
Does the automatic conversion of Integer into Long happen in Excel as well as Word
Well, I'd say it's a VBA feature, the Application doesn't matter.
 
Yeah, that is - as stated - what I assumed...but I have been caught with differences before. What do I know? I thought it was possible Excel (as it is so numbers based) may be enabled with some sort of an exception. After all, this is going on behind the scenses anyway.

Gerry
 
I always use longs just in case...there's likely to be far bigger memory issues with the other objects I use anyway!!

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top