Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
APH APH is offline
external usenet poster
 
Posts: 1
Default Dimension problem

Hi - can someone explain the following please?

if I set up:

dim i , j, k, As Integer

i = 3.7
j = 3.7
k = 3.7

MsgBox i
MsgBox j
MsgBox k

Do the first two MsgBox show 3.7 and only the third as 4.00

In my basic understanding I am assuming that VB will only dim the variable
immediately to the left of As Integer and for the others it waits to set
their type until it comes across them. If this is right, is it bad
programming to declare similar cariable types on a single linbe

thanks

Alex



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Dimension problem

Alex,

VB is doing exactly what it should. When you declare variables
with the syntax

dim i , j, k, As Integer

only the variable k is an integer. I and j are Variants. Your
syntax is the same as

Dim i As Variant, j As Variant, k As Integer

When you assign a value of 3.7 to i and j, VBA gives the variant
a subtype of Double. Whether you should declare more than one
variable on a single line of code is a matter of personal
programming style. I don't like it, but others do. Go with your
own style.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"APH" wrote in message
...
Hi - can someone explain the following please?

if I set up:

dim i , j, k, As Integer

i = 3.7
j = 3.7
k = 3.7

MsgBox i
MsgBox j
MsgBox k

Do the first two MsgBox show 3.7 and only the third as 4.00

In my basic understanding I am assuming that VB will only dim

the variable
immediately to the left of As Integer and for the others it

waits to set
their type until it comes across them. If this is right, is

it bad
programming to declare similar cariable types on a single linbe

thanks

Alex





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 391
Default Dimension problem


dim i , j, k, As Integer
.....would raise an error !!
Otherwise I'd agree with Chip, plus I'd add two further
comments: One would be that more descriptive variable
names make code much clearer to debug, and second, that
adding
Option Explicit
at the top of the module is a MUST...thuis can be set
automatically under the IDE Tools/Options Editor tab and
checking the 'Require Variable Declaration' box.

best regarsd

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Alex,

VB is doing exactly what it should. When you declare

variables
with the syntax

dim i , j, k, As Integer

only the variable k is an integer. I and j are

Variants. Your
syntax is the same as

Dim i As Variant, j As Variant, k As Integer

When you assign a value of 3.7 to i and j, VBA gives the

variant
a subtype of Double. Whether you should declare more

than one
variable on a single line of code is a matter of personal
programming style. I don't like it, but others do. Go

with your
own style.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"APH" wrote in message
...
Hi - can someone explain the following please?

if I set up:

dim i , j, k, As Integer

i = 3.7
j = 3.7
k = 3.7

MsgBox i
MsgBox j
MsgBox k

Do the first two MsgBox show 3.7 and only the third as

4.00

In my basic understanding I am assuming that VB will

only dim
the variable
immediately to the left of As Integer and for the

others it
waits to set
their type until it comes across them. If this is

right, is
it bad
programming to declare similar cariable types on a

single linbe

thanks

Alex





.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Dimension problem

Thanks Chip - I have learnt something today.....
I tend to always declare on a separate line, but this arose as I was at a
training sessions yesterday, when the trainer ran in to the problem in his
own code....

It was just for my own personal knowledge.. thanks again

Alex


"Chip Pearson" wrote in message
...
Alex,

VB is doing exactly what it should. When you declare variables
with the syntax

dim i , j, k, As Integer

only the variable k is an integer. I and j are Variants. Your
syntax is the same as

Dim i As Variant, j As Variant, k As Integer

When you assign a value of 3.7 to i and j, VBA gives the variant
a subtype of Double. Whether you should declare more than one
variable on a single line of code is a matter of personal
programming style. I don't like it, but others do. Go with your
own style.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"APH" wrote in message
...
Hi - can someone explain the following please?

if I set up:

dim i , j, k, As Integer

i = 3.7
j = 3.7
k = 3.7

MsgBox i
MsgBox j
MsgBox k

Do the first two MsgBox show 3.7 and only the third as 4.00

In my basic understanding I am assuming that VB will only dim

the variable
immediately to the left of As Integer and for the others it

waits to set
their type until it comes across them. If this is right, is

it bad
programming to declare similar cariable types on a single linbe

thanks

Alex







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Dimension problem

Thanks Patrick -
Please see my response to Chip - and I agree, I tend to use far more
descriptive variable anmes - I hate looking at code that uses just a single
letter.

I am assumiong that Option Explicit, forces you to delcare variable before
you use them.

Alex

"Patrick Molloy" wrote in message
...

dim i , j, k, As Integer
....would raise an error !!
Otherwise I'd agree with Chip, plus I'd add two further
comments: One would be that more descriptive variable
names make code much clearer to debug, and second, that
adding
Option Explicit
at the top of the module is a MUST...thuis can be set
automatically under the IDE Tools/Options Editor tab and
checking the 'Require Variable Declaration' box.

best regarsd

Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Alex,

VB is doing exactly what it should. When you declare

variables
with the syntax

dim i , j, k, As Integer

only the variable k is an integer. I and j are

Variants. Your
syntax is the same as

Dim i As Variant, j As Variant, k As Integer

When you assign a value of 3.7 to i and j, VBA gives the

variant
a subtype of Double. Whether you should declare more

than one
variable on a single line of code is a matter of personal
programming style. I don't like it, but others do. Go

with your
own style.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"APH" wrote in message
...
Hi - can someone explain the following please?

if I set up:

dim i , j, k, As Integer

i = 3.7
j = 3.7
k = 3.7

MsgBox i
MsgBox j
MsgBox k

Do the first two MsgBox show 3.7 and only the third as

4.00

In my basic understanding I am assuming that VB will

only dim
the variable
immediately to the left of As Integer and for the

others it
waits to set
their type until it comes across them. If this is

right, is
it bad
programming to declare similar cariable types on a

single linbe

thanks

Alex





.





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 97
Default Dimension problem

"Patrick Molloy" wrote...
dim i , j, k, As Integer
....would raise an error !!


Odds are Chip missed the original typo.

. . . One would be that more descriptive variable
names make code much clearer to debug, . . .


Begging the question how descriptive array indices need to be. Constants and
long lived variables should have descriptive names. Short lived variables
usually don't need them.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 dimension chart jack Charts and Charting in Excel 2 December 9th 08 01:43 AM
two dimension look up table Dave Jepson Excel Worksheet Functions 1 October 26th 06 01:03 PM
3 dimension lookup problem barkiny Excel Worksheet Functions 1 March 14th 06 03:05 PM
Matrix Dimension Michelle Excel Worksheet Functions 1 November 24th 05 05:45 PM
Using the sum function in a 3 dimension environment Paul Hargreaves Excel Discussion (Misc queries) 2 February 23rd 05 12:29 AM


All times are GMT +1. The time now is 11:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"