Assigning values to an array of records

O

Ogier

I would like to assign values to an array consisting of records with three
string fields:


Public Type Shortcut
strF1 As String
strF2 As String
strF3 As String
End Type

Dim GreekLetters(53) As Shortcut

Sub Assingments()
GreekLetters(1).strF1 = "1F1"
GreekLetters(1).strF2 = "1F2"
GreekLetters(1).strF3 = "1F3"
...
End Sub


Is it possible to shorten this? The following does *not* work, the compiler
expects a ")" at the first comma:

GreekLetters(1) = ("1F1","1F1","1F1")

I tried to change the record into an array, Dim GreekLetters(53,3) As
String, but this also gives a compiler error when using a one-line assignment.

Best wishes
Holger Nielsen
 
D

Doug Robbins - Word MVP

I do not follow what you are trying to do, but

Dim GreekLetters as Variant
GreekLetters = Split("1F1,1F2,1F3", ",")

will load an array with the name GreekLettes with the elements "1F1", "1F2"
and "1F3"

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
O

Ogier

Doug Robbins - Word MVP said:
I do not follow what you are trying to do, but...

Well, suppose I have
Dim GreekLetters(53, 3) As String

I could then assign values like this:

Sub AssignGreekLetters()
GreekLetters(1, 1) = "This"
GreekLetters(1, 2) = "That"
GreekLetters(1, 3) = "Thus"
GreekLetters(2, 1) = "This"
GreekLetters(2, 2) = "That"
GreekLetters(2, 3) = "Thus"
... many more lines!
End Sub

I was just wondering, if there is a more compact way like

Sub AssignGreekLetters()
GreekLetters(1) = ("This", "That", "Thus")
GreekLetters(2) = (...)
...
End Sub

Holger
 
D

Doug Robbins - Word MVP

If you were to put the values to be loaded into the array into and Excel
spreadsheet and you assign the name arraydata to that range, you can then
use


GreekLetters = ActiveSheet.Range("arraydata")

to load the data into the array.

That of course can be automated from Word if that is where you want to make
use of it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
D

Doug Robbins - Word MVP

Another way to load the array is if the data is in a table in a Word
document

Dim tbl As Table, i As Long, j As Long, m As Long, n As Long, dataitem As
Range
Set tbl = ActiveDocument.Tables(1)
With tbl
i = .Rows.Count
j = .Columns.Count
ReDim GreekLetters(i, j)
For m = 1 To i
For n = 1 To j
Set dataitem = .Cell(m, n).Range
dataitem.End = dataitem.End - 1
GreekLetters(m, n) = dataitem
Next n
Next m
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
T

That Guy

I would like to assign values to an array consisting of records with three
string fields:

Public Type Shortcut
    strF1 As String
    strF2 As String
    strF3 As String
    End Type

Dim GreekLetters(53) As Shortcut

Sub Assingments()
    GreekLetters(1).strF1 = "1F1"
    GreekLetters(1).strF2 = "1F2"
    GreekLetters(1).strF3 = "1F3"
        ...
End Sub

Is it possible to shorten this? The following does *not* work, the compiler
expects a ")" at the first comma:

    GreekLetters(1) = ("1F1","1F1","1F1")

I tried to change the record into an array, Dim GreekLetters(53,3) As
String, but this also gives a compiler error when using a one-line assignment.

Best wishes
Holger Nielsen

Not the shortest amount of code but you could always right a sub for
assignment:

i.e.

private sub assign(byref useShort as shortcut, byval one as string,
byval two as string, byval three as string)
useshort.str1 = one
useshort.str2 = two
useshort.str3 = three
end sub

ans then call it with:

assign Greekletters(1), "1F1","1F2","1F3"

That would shorten the inline calls but require more upfront setup.

good luck
 
O

Ogier

:

....
Not the shortest amount of code but you could always right a sub for
assignment:

i.e.

private sub assign(byref useShort as shortcut, byval one as string,
byval two as string, byval three as string)
useshort.str1 = one
useshort.str2 = two
useshort.str3 = three
end sub

ans then call it with:

assign Greekletters(1), "1F1","1F2","1F3"

That would shorten the inline calls but require more upfront setup.

That was precicely the solution I had hoped for, thank you very much!

The suggestions by the previous posters are interesting and new to me. I
wont't use then at this time because they utilise outside programs.

Best wishes
Holger
 

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