PC Review


Reply
Thread Tools Rate Thread

Declaring Multi-Dimensional Arrays

 
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      26th Jul 2007
I would like to declare a multi-dimensional array with different types, such
as:

dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date

Can this be done or should I leave it as Variant? After reading Mr.
Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
variables I'm hesitant to leave it...
--
Adios,
Clay Harryman
 
Reply With Quote
 
 
 
 
RB Smissaert
Guest
Posts: n/a
 
      26th Jul 2007
You can't declare an array with different types, but maybe an array of UDT's
could help you out:

Option Explicit
Private Type ColumnData
lCol1 As Long
strCol2 As String
daCol3 As Date
daCol4 As Date
End Type

Sub test()

Dim uCData As ColumnData
Dim arr(1 To 10) As ColumnData

uCData.lCol1 = 1
uCData.strCol2 = "test"
uCData.daCol3 = "24/04/2003"
uCData.daCol4 = "25/04/2003"

arr(1) = uCData

MsgBox Weekday(arr(1).daCol3)

End Sub


RBS


"Clayman" <(E-Mail Removed)> wrote in message
news:A307B10E-7382-4E3C-9A81-(E-Mail Removed)...
>I would like to declare a multi-dimensional array with different types,
>such
> as:
>
> dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date
>
> Can this be done or should I leave it as Variant? After reading Mr.
> Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
> variables I'm hesitant to leave it...
> --
> Adios,
> Clay Harryman


 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      26th Jul 2007
Thank you. This looks like it will work. I'm not in a position to try it
right now, but I'll let you know.
--
Adios,
Clay Harryman


"RB Smissaert" wrote:

> You can't declare an array with different types, but maybe an array of UDT's
> could help you out:
>
> Option Explicit
> Private Type ColumnData
> lCol1 As Long
> strCol2 As String
> daCol3 As Date
> daCol4 As Date
> End Type
>
> Sub test()
>
> Dim uCData As ColumnData
> Dim arr(1 To 10) As ColumnData
>
> uCData.lCol1 = 1
> uCData.strCol2 = "test"
> uCData.daCol3 = "24/04/2003"
> uCData.daCol4 = "25/04/2003"
>
> arr(1) = uCData
>
> MsgBox Weekday(arr(1).daCol3)
>
> End Sub
>
>
> RBS
>
>
> "Clayman" <(E-Mail Removed)> wrote in message
> news:A307B10E-7382-4E3C-9A81-(E-Mail Removed)...
> >I would like to declare a multi-dimensional array with different types,
> >such
> > as:
> >
> > dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date
> >
> > Can this be done or should I leave it as Variant? After reading Mr.
> > Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
> > variables I'm hesitant to leave it...
> > --
> > Adios,
> > Clay Harryman

>
>

 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      26th Jul 2007
Hi Clay,
Is there any reason you cannot to use a pair of 1D arrays for your Longs and
Strings and a 2D array for your dates. However 200x4 array is not large and
I doubt you'd notice any performance loss by declaring as a Variant.

If you particularly want to fix mixed data types within the array, eg for
coercing to correct data type, you could do -

Dim v(1 To 4)
Dim arrL(1 To 200) As Long
Dim arrS(1 To 200) As String
Dim arrD1(1 To 200) As Date, arrD2(1 To 200) As Date
'arrL(1) = 123
'arrS(1) = "abc"
'arrD1(1) = Date
'arrD2(1) = Date + 1
v(1) = arrL
v(2) = arrS
v(3) = arrD1
v(4) = arrD2

v(1)(2) = 6.789
MsgBox v(1)(2) ' 7 rounded to a long

v(3)(2) = CLng(Date) ' a number
MsgBox v(3)(2) ' a date

v(1)(3) = "abc" ' error type mismatch, expecting a long

Regards,
Peter T

"Clayman" <(E-Mail Removed)> wrote in message
news:A307B10E-7382-4E3C-9A81-(E-Mail Removed)...
> I would like to declare a multi-dimensional array with different types,

such
> as:
>
> dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date
>
> Can this be done or should I leave it as Variant? After reading Mr.
> Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
> variables I'm hesitant to leave it...
> --
> Adios,
> Clay Harryman



 
Reply With Quote
 
=?Utf-8?B?Q2xheW1hbg==?=
Guest
Posts: n/a
 
      27th Jul 2007
This question may make the arrays moot:

I'm using this array to store vacation taken/sold information, and make it
available in a series of ListBoxes for editing. The main reason I wanted the
array was to store (invisible to the user) the transaction ID for each
record. This makes for bulky code, though.

Is it bad programming practice to store the values in the ListBoxes - not
the arrays - including an invisible ListBox to store the transaction ID? I
mean, I can hear my old CS professors screaming not to do it, but I don't
know if those voices are right...
--
Adios,
Clay Harryman


"RB Smissaert" wrote:

> You can't declare an array with different types, but maybe an array of UDT's
> could help you out:
>
> Option Explicit
> Private Type ColumnData
> lCol1 As Long
> strCol2 As String
> daCol3 As Date
> daCol4 As Date
> End Type
>
> Sub test()
>
> Dim uCData As ColumnData
> Dim arr(1 To 10) As ColumnData
>
> uCData.lCol1 = 1
> uCData.strCol2 = "test"
> uCData.daCol3 = "24/04/2003"
> uCData.daCol4 = "25/04/2003"
>
> arr(1) = uCData
>
> MsgBox Weekday(arr(1).daCol3)
>
> End Sub
>
>
> RBS
>
>
> "Clayman" <(E-Mail Removed)> wrote in message
> news:A307B10E-7382-4E3C-9A81-(E-Mail Removed)...
> >I would like to declare a multi-dimensional array with different types,
> >such
> > as:
> >
> > dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date
> >
> > Can this be done or should I leave it as Variant? After reading Mr.
> > Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
> > variables I'm hesitant to leave it...
> > --
> > Adios,
> > Clay Harryman

>
>

 
Reply With Quote
 
LOFE
Guest
Posts: n/a
 
      10th Aug 2009
I guess it might be a bit late, but in the listbox, you can always set the
column that you don't want the user to see to 0 length. That way you still
get all the data that you need without going through the arrays and the
ListBox is then just populated through ListBox1.RowSource = Range("RC:RC").

ListBox1.ColumnCount = 3
ListBox1.ColumnWidths = 90;0;90

"Clayman" wrote:

> This question may make the arrays moot:
>
> I'm using this array to store vacation taken/sold information, and make it
> available in a series of ListBoxes for editing. The main reason I wanted the
> array was to store (invisible to the user) the transaction ID for each
> record. This makes for bulky code, though.
>
> Is it bad programming practice to store the values in the ListBoxes - not
> the arrays - including an invisible ListBox to store the transaction ID? I
> mean, I can hear my old CS professors screaming not to do it, but I don't
> know if those voices are right...
> --
> Adios,
> Clay Harryman
>
>
> "RB Smissaert" wrote:
>
> > You can't declare an array with different types, but maybe an array of UDT's
> > could help you out:
> >
> > Option Explicit
> > Private Type ColumnData
> > lCol1 As Long
> > strCol2 As String
> > daCol3 As Date
> > daCol4 As Date
> > End Type
> >
> > Sub test()
> >
> > Dim uCData As ColumnData
> > Dim arr(1 To 10) As ColumnData
> >
> > uCData.lCol1 = 1
> > uCData.strCol2 = "test"
> > uCData.daCol3 = "24/04/2003"
> > uCData.daCol4 = "25/04/2003"
> >
> > arr(1) = uCData
> >
> > MsgBox Weekday(arr(1).daCol3)
> >
> > End Sub
> >
> >
> > RBS
> >
> >
> > "Clayman" <(E-Mail Removed)> wrote in message
> > news:A307B10E-7382-4E3C-9A81-(E-Mail Removed)...
> > >I would like to declare a multi-dimensional array with different types,
> > >such
> > > as:
> > >
> > > dim array(200,4) where (x,1)=Long, (x,2)=String, (x,3)=Date, (x,4)=Date
> > >
> > > Can this be done or should I leave it as Variant? After reading Mr.
> > > Pearson's page (http://www.cpearson.com/excel/variables.htm) regarding
> > > variables I'm hesitant to leave it...
> > > --
> > > Adios,
> > > Clay Harryman

> >
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring 2 dimensional arrays Dan Microsoft Excel Programming 4 17th Nov 2005 08:36 PM
Declaring 2 dimensional arrays Dan Microsoft Access Macros 1 15th Nov 2005 10:06 PM
Multi Dimensional Arrays DazedAndConfused Microsoft VB .NET 4 26th Jul 2005 11:08 AM
Multi dimensional arrays in C# and in C++ =?Utf-8?B?SGFucw==?= Microsoft VC .NET 7 13th May 2005 11:30 PM
Declaring Dynamic Multi-dimensional Array JohnV Microsoft Excel Programming 2 15th Jul 2003 06:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:35 PM.