ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Give array values (https://www.excelbanter.com/excel-programming/410990-re-give-array-values.html)

Rick Rothstein \(MVP - VB\)[_1938_]

Give array values
 
You can use a structure like this to load up your array...

Const NumberOfRows As Long = 6
Const NumberOfCols As Long = 6
Dim ColNum As Long
Dim RowNum As Long
Dim Temp() As String
Dim MyData(1 To NumberOfRows, 1 To NumberOfCols) As String
Temp = Split("New York,France,John,..etc...,Radio", ",")
For ColNum = 1 To NumberOfCols
For RowNum = 1 To NumberOfRows
MyData(RowNum, ColNum) = Temp(NumberOfCols * (RowNum - 1) + ColNum - 1)
Next
Next
Erase Temp

This works as long both dimensions of the MyData array start with 1....
simply assign the number of rows and columns to the appropriate Const
statements and put your list of items (across each row, column-by-column
ordering), comma delimited, into the first argument of the Split function
(do not try to pretty things up by placing spaces around the delimiter; and,
if any of your items have internal commas, you will need to use a different
delimiter, one that is not included within any items in the list, and change
the second argument of the Split function to reflect what it is).

Rick


"shapper" wrote in message
...
Hello,

I created an array of string as follows:

Dim MyData(1 To 6, 1 To 6) As String

Then I gave values to the 36 array elements as follows:
MyData(1,1) = "New York"
MyData(1,2) = "France"
MyData(1,3) = "John"
...
MyData(6,6) = "Radio"

Is there a way to fill the 36 elements instead of using one code line
for each array element?

Thanks,
Miguel



Alan Beban[_2_]

Give array values
 
Or, if the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook

Temp = Split("New York, France, John, etc, Radio", ",")
arr = ArrayReshape(Temp, 6, 6)

Alan Beban

Rick Rothstein (MVP - VB) wrote:
You can use a structure like this to load up your array...

Const NumberOfRows As Long = 6
Const NumberOfCols As Long = 6
Dim ColNum As Long
Dim RowNum As Long
Dim Temp() As String
Dim MyData(1 To NumberOfRows, 1 To NumberOfCols) As String
Temp = Split("New York,France,John,..etc...,Radio", ",")
For ColNum = 1 To NumberOfCols
For RowNum = 1 To NumberOfRows
MyData(RowNum, ColNum) = Temp(NumberOfCols * (RowNum - 1) + ColNum - 1)
Next
Next
Erase Temp

This works as long both dimensions of the MyData array start with 1....
simply assign the number of rows and columns to the appropriate Const
statements and put your list of items (across each row, column-by-column
ordering), comma delimited, into the first argument of the Split
function (do not try to pretty things up by placing spaces around the
delimiter; and, if any of your items have internal commas, you will need
to use a different delimiter, one that is not included within any items
in the list, and change the second argument of the Split function to
reflect what it is).

Rick


"shapper" wrote in message
...

Hello,

I created an array of string as follows:

Dim MyData(1 To 6, 1 To 6) As String

Then I gave values to the 36 array elements as follows:
MyData(1,1) = "New York"
MyData(1,2) = "France"
MyData(1,3) = "John"
...
MyData(6,6) = "Radio"

Is there a way to fill the 36 elements instead of using one code line
for each array element?

Thanks,
Miguel





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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com