Dynamically dimensioning/filling a multidimensional array

ease20022002

Board Regular
Joined
Jun 4, 2005
Messages
233
Hi,

I am writing in VB 6.0. I do not want to dimension a multi-dimensional array by writing:

Code:
varTable(0, 0) = LossCentral 
varTable(0, 1) = PremCentral
varTable(1, 0) = LossArchive
varTable(1, 1) = PremArchive

I would like to write something like VB.Net:

Code:
Dim rectArray(4, 2) As Integer
'declares an array of 5 by 3 members which is a 15 member array
Dim rectArray(,) As Integer = {{1, 2, 3}, {12, 13, 14}, {11, 10, 9}}
'setting initial values

Is there anyway to get anywhere close to this in VB 6.0?

Thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi,

some ideas:
Code:
Sub test1()

Dim rectArray() As Variant
'or
'Dim rectArray As Variant
Dim arr As Variant
rectArray = Array(Array(1, 2, 3), Array(12, 13, 14), Array(11, 10, 9))
    With Application
    arr = .Transpose(.Transpose(rectArray))
    Range("A1:C3") = arr
    End With

End Sub

Sub test2()

Dim rectArray() As Variant
'or
'Dim rectArray As Variant
rectArray = Range("D1:F3")

Range("A1:C3") = rectArray

End Sub
hoping others will jump in to provide more Dim and Redim details ...

kind regards,
Erik
 
L

Legacy 98055

Guest
You would need to create a user defined function to assign directly to strong types. The Dim statement is executable in VB net but not in previous versions. Hence you can only assign constants to constants.

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#008000">'this is acceptable syntax in VB 6</font>
  <font color="#0000A0">Dim</font> rectArray(4, 2) <font color="#0000A0">As</font> <font color="#0000A0">Integer</font>

  <font color="#008000">'Dim rectArray(,) As Integer = {{1, 2, 3}, {12, 13, 14}, {11, 10, 9}}</font>
  <font color="#008000">'VB Net results in array structure</font>
  Console.WriteLine (rectArray(0, 0))
  Console.WriteLine (rectArray(0, 1))
  Console.WriteLine (rectArray(0, 2))

  <font color="#008000">'must be a variant because the Array function only returns variants</font>
  <font color="#0000A0">Dim</font> rectArray()
  rectArray = Array(Array(1, 2, 3), Array(12, 13, 14), Array(11, 10, 9))
  <font color="#008000">'pre vb net results. you get a single</font>
  <font color="#008000">'variant array containing 3 variant arrays</font>
  Debug.Print rectArray(0)(0)
  Debug.Print rectArray(0)(1)
  Debug.Print rectArray(0)(2)
</FONT></td></tr></table><button onclick='document.all("922200619354703").value=document.all("922200619354703").value.replace(/<br \/>\s\s/g,"");document.all("922200619354703").value=document.all("922200619354703").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("922200619354703").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="922200619354703" wrap="virtual">
'this is acceptable syntax in VB 6
Dim rectArray(4, 2) As Integer

'Dim rectArray(,) As Integer = {{1, 2, 3}, {12, 13, 14}, {11, 10, 9}}
'VB Net results in array structure
Console.WriteLine (rectArray(0, 0))
Console.WriteLine (rectArray(0, 1))
Console.WriteLine (rectArray(0, 2))

'must be a variant because the Array function only returns variants
Dim rectArray()
rectArray = Array(Array(1, 2, 3), Array(12, 13, 14), Array(11, 10, 9))
'pre vb net results. you get a single
'variant array containing 3 variant arrays
Debug.Print rectArray(0)(0)
Debug.Print rectArray(0)(1)
Debug.Print rectArray(0)(2)</textarea>
 

Forum statistics

Threads
1,136,268
Messages
5,674,734
Members
419,523
Latest member
Urnovio

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top