Load a 2 dimensional array

S

Steve Cronin

Folks;

Is there a better way to load a two dimensional array than:

tagArray(0,0) = \"<SOrg>\"
tagArray(1,0) = \"<SFullName>\"
tagArray(2,0) = \"<SAddr>\"
tagArray(3,0) = \"<SCSZ>\"
.... 50 more lines
tagArray(0,1) = \"<#$Org>\"
tagArray(1,1) = \"<#$FullName>\"
tagArray(2,1) = \"<#$Addr>\"
tagArray(3,1) = \"<#$CSZ>\"
.... 50 more lines

Thanks,
Steve
 
J

Jezebel

One neat method is to put the data into an Excel worksheet. With a reference
to the worksheet (you need to add a reference to Excel to your Word VBA) and
assuming that the data is in columns 1 and 2, you can use ---

Dim pLastRow as long
Dim ptagArray() as variant

'Find the last row containing data
With xlSheet.UsedRange
pLastRow= .Cells(.Rows.Count,1).Row
End With

'Transfer the data to the array
ptagArray = xlSheet.Range("A1:B" & pLastRow)
 
J

Jezebel

It depends on what you're actually trying to achieve: less code? easier
maintenance?
 
A

Andy Fish

Ah - I remember the good old days of BASIC with the DATA statement.

that was one of the best things about BASIC, and I don't know of any other
language that has it. I was quite miffed they left it out of VB
 
H

Helmut Weber

Hi Steve,

you could split a string into an array,
let's say the string is:

MyString = "\<SOrg>\" & "\<SFullName>\" & "\<SAddr>\" & ...
MyString = replace(mystring, "\\", "\ \")
myarray = split(MyString, " ")

See help for split, again available in Word 2000 or higher, I suppose.

All untested!

Though You'll have to use two arryas, IMHO.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"








like myarr() = split
 
J

Jonathan West

Hi Steve

Put your list of tags into one long string, using a delimiter character
between elements not used in the individual elements.

Suppose you use a comma as your delimiter, you can then split the text into
a one-dimensional array in a variant like this.

Dim vTemp as Variant
vTemp = Split(strMyData, ",")

You can then dimension your array of tags to match the number of elements in
vTemp, and copy across, like this

ReDim tagArray(3, UBound(vTemp) \ 4)
For i = 0 to UBound(vTemp)
tagArray(i Mod 4, i \ 4) = vTemp(i)
Next i

Note the use of \ rather than / for integer division.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
S

Steve Cronin

Folks;

Hmm. I running this in Word vX on a Mac.
There is no Split function available there.
Split method applies to Tables and Cells but not strings.

Both alternates relied on split to do the heavy lifting of cracking a
long string...
So I guess I am out of luck?

Steve
 
H

Helmut Weber

Hi Steve,

if "split" is missing from your VBA-version,
"replace" might be missing as well.

This is from German fellow MVP Christian Freßdorf:

Public Function Replace(sIn As String, _
ByVal sFind As String, _
ByVal sReplace As String, _
Optional ByVal nStart As Long = 1, _
Optional ByVal nCount As Long = -1, _
Optional ByVal bCompare As Variant = vbBinaryCompare) As String
Dim nC As Long, nPos As Long, sOut As String
sOut = sIn
If sFind = sReplace Then GoTo EndFn
nPos = InStr(nStart, sOut, sFind, bCompare)
If nPos = 0 Then GoTo EndFn
Do
nC = nC + 1
sOut = Left(sOut, nPos - 1) & _
sReplace & Mid(sOut, nPos + Len(sFind))
If nCount <> -1 And nC >= nCount Then Exit Do
nPos = InStr(nPos + Len(sReplace), sOut, sFind, bCompare)
Loop While nPos > 0
EndFn:
Replace = sOut
End Function

Beware of linebreaks by the newsreader.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jezebel

I think you might be losing the plot here. With 50 pairs of values to deal
with, maintenance is likely to be your biggest challenge. A packed string
might be easy to load, but it will be a bitch-and-a-half to maintain. Your
original approach, albeit ugly, is probably less total effort.
 
H

Helmut Weber

Hi Jezebel,

right you are.

I'd set up a simple text-file with pairs of values.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top