Change Array to Multidimensional Array 
Author Message
 Change Array to Multidimensional Array

I found a split function to use with a delimited file (because I'm
using Word 97 so can't use Split())

At the end of the function I have each item from the file stored in an
array but I need it to be in a multidimensional array so that I can
use it elsewhere.

The file looks as follows:

Item1,tel1,fax1
Item2,tel2,fax2
Item3,tel3,fax3

And when in the array it looks like:

Item1,tel1,fax1,Item2,tel2,fax2,Item3,tel3,fax3

However I would like it to be like:

MyArray(0)(0) = Item1
MyArray(0)(1) = tel1
MyArray(0)(2) = fax1
MyArray(1)(0) = Item2
etc
etc

Could you show/point me to a method to move the items from the
original array to the multidimensional array

OR alternatively could you show me how to modify the function to
insert each item correctly in the multi array

Function SplitString(Value As String, Optional SplitChr As String =
";") As Variant

On Error Resume Next

Dim Pos As Long
Dim Start As Long
Dim Length As Long
Dim Cnt As Long
Dim Result As String
Dim ResultArray() As Variant

   ReDim Preserve ResultArray(Cnt) As Variant
   Start = 1
   Do
      Value = Trim(Value)
      Length = Len(Value)
      If Length = 0 Then Exit Do
      Pos = InStr(Start, Value, SplitChr)
      Select Case Pos
         Case 0
            ReDim Preserve ResultArray(Cnt) As Variant
            If (Not Value = Empty) Then ResultArray(Cnt) = Value
            Exit Do
         Case Else
            Result = Strings.Left(Value, Pos - 1)
            If (Not Result = Empty) Then
               ReDim Preserve ResultArray(Cnt) As Variant
               ResultArray(Cnt) = Result
               Cnt = Cnt + 1
            End If
            Value = Right(Value, Length - Pos)
      End Select
   Loop

SplitString = ResultArray

End Function

Any help is most appreciated
thnxs

Mark.



Tue, 05 Oct 2004 22:59:20 GMT  
 Change Array to Multidimensional Array
Hi Mark

I'll take the simpler approach of moving the items from ResultArray into
MyArray, having correctly dimensioned MyArray

Dim MyArray() as String
Dim i as Long
Dim j as Long
Dim k as Long
ReDim MyArray(UBound(ResultArray) \ 3), 2)
k = 0
For i = 0 To UBound(MyArray, 1)
    For j = 0 to 2
        MyArray(i, j) = ResultArray(k)
        k = k + 1
    Next j
Next i

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> I found a split function to use with a delimited file (because I'm
> using Word 97 so can't use Split())

> At the end of the function I have each item from the file stored in an
> array but I need it to be in a multidimensional array so that I can
> use it elsewhere.

> The file looks as follows:

> Item1,tel1,fax1
> Item2,tel2,fax2
> Item3,tel3,fax3

> And when in the array it looks like:

> Item1,tel1,fax1,Item2,tel2,fax2,Item3,tel3,fax3

> However I would like it to be like:

> MyArray(0)(0) = Item1
> MyArray(0)(1) = tel1
> MyArray(0)(2) = fax1
> MyArray(1)(0) = Item2
> etc
> etc

> Could you show/point me to a method to move the items from the
> original array to the multidimensional array

> OR alternatively could you show me how to modify the function to
> insert each item correctly in the multi array

> Function SplitString(Value As String, Optional SplitChr As String =
> ";") As Variant

> On Error Resume Next

> Dim Pos As Long
> Dim Start As Long
> Dim Length As Long
> Dim Cnt As Long
> Dim Result As String
> Dim ResultArray() As Variant

>    ReDim Preserve ResultArray(Cnt) As Variant
>    Start = 1
>    Do
>       Value = Trim(Value)
>       Length = Len(Value)
>       If Length = 0 Then Exit Do
>       Pos = InStr(Start, Value, SplitChr)
>       Select Case Pos
>          Case 0
>             ReDim Preserve ResultArray(Cnt) As Variant
>             If (Not Value = Empty) Then ResultArray(Cnt) = Value
>             Exit Do
>          Case Else
>             Result = Strings.Left(Value, Pos - 1)
>             If (Not Result = Empty) Then
>                ReDim Preserve ResultArray(Cnt) As Variant
>                ResultArray(Cnt) = Result
>                Cnt = Cnt + 1
>             End If
>             Value = Right(Value, Length - Pos)
>       End Select
>    Loop

> SplitString = ResultArray

> End Function

> Any help is most appreciated
> thnxs

> Mark.



Tue, 05 Oct 2004 23:24:51 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Multidimensional, multidimensional arrays

2. Sort Multidimensional Array By Any Element?

3. linking multidimensional arrays???

4. Declaring multidimensional dynamic arrays

5. Multidimensional Array

6. multidimensional arrays

7. web delimited file to multidimensional array

8. How to declare function to return multidimensional array

9. Sorting with MultiDimensional Array

10. Grab first column in multidimensional array

11. NEWBIE QUESTION: ReDim Preserver Multidimensional Arrays ??

12. dynamic multidimensional array?

 

 
Powered by phpBB® Forum Software