Creating a two dimensional array from single arrays

J

Joanne

Hello, I am trying to take the information from two single dimensional arrays
(arrNames and arrExtensions) and combine them into a two dimensional array
(arrPartnerList). The routine seems to work fine until I get to the actual
combination where I assign a value to arrPartnerList. I know my logic is
wrong, or I don't understand two-dimensional arrays. Could somebody point me
in the right direction? Thanks very much.

Public Sub PartnerInfo()
'Declare Arrays for Partner and Extensions
Dim arrNames() As String
Dim arrExtensions() As String
Dim arrPartnerList(3, 3) As String

Dim p As Integer
Dim e As Integer

p = 0
e = 0
For p = 0 To 2
arrNames() = Split("John Smith|Mike Love|William Jones", "|")
strPartner = arrNames(p)
MsgBox strPartner
For e = 0 To 2
arrExtensions() = Split("816|637|5102","|")
strExtensions = arrExtensions(e)
MsgBox strExtensions
arrPartnerList(p, e) = (strPartner, strExtensions)

MsgBox arrPartnerList(p, e)

Next e
Next p
 
G

Greg Maxey

Joanne,

Basically like this:

Public Sub PartnerInfo()
Dim arrNames() As String
Dim arrExtensions() As String
Dim arrPartnerList() As String
Dim j As Long
Dim i As Long
arrNames() = Split("John Smith|Mike Love|William Jones", "|")
arrExtensions() = Split("816|637|5102", "|")
If UBound(arrNames) <> UBound(arrExtensions) Then
MsgBox "This train is about to wreak"
End If
ReDim arrPartnerList(1, UBound(arrNames))
For i = 0 To UBound(arrNames)
arrPartnerList(0, i) = arrNames(i)
arrPartnerList(1, i) = arrExtensions(i)
Next i
'Validate
For i = 0 To UBound(arrPartnerList, 2)
MsgBox arrPartnerList(0, i) & " Extension: " & arrPartnerList(1, i)
Next i
End Sub

Note: You may want to add some error handling because if the two arrays are
not equal in scope the train will wreck.

Hello, I am trying to take the information from two single
dimensional arrays (arrNames and arrExtensions) and combine them into
a two dimensional array (arrPartnerList). The routine seems to work
fine until I get to the actual combination where I assign a value to
arrPartnerList. I know my logic is wrong, or I don't understand
two-dimensional arrays. Could somebody point me in the right
direction? Thanks very much.

Public Sub PartnerInfo()
'Declare Arrays for Partner and Extensions
Dim arrNames() As String
Dim arrExtensions() As String
Dim arrPartnerList(3, 3) As String

Dim p As Integer
Dim e As Integer

p = 0
e = 0
For p = 0 To 2
arrNames() = Split("John Smith|Mike Love|William Jones", "|")
strPartner = arrNames(p)
MsgBox strPartner
For e = 0 To 2
arrExtensions() = Split("816|637|5102","|")
strExtensions = arrExtensions(e)
MsgBox strExtensions
arrPartnerList(p, e) = (strPartner, strExtensions)

MsgBox arrPartnerList(p, e)

Next e
Next p

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Gordon Bentley-Mix

Joanne,

Rather than try to explain what you've done wrong, I'm just going to show
you the right way and let you work it out from there if that's OK. If you
have any questions just post back.

Sub PartnerInfo()
Dim arrNames() As String
Dim arrExtensions() As String
arrNames = Split("John Smith|Mike Love|William Jones", "|")
arrExtensions = Split("816|637|5102", "|")
If UBound(arrNames) <> UBound(arrExtensions) Then
MsgBox "Number of names not equal to number of extensions"
Exit Sub
Else
Dim arrPartnerList() As String
Dim i As Long
Dim n As Long
n = UBound(arrNames) 'or UBound(arrExtensions) - makes no real
difference
ReDim arrPartnerList(1, n) As String
For i = 0 To n
arrPartnerList(0, i) = arrNames(i)
arrPartnerList(1, i) = arrExtensions(i)
Next i
End If
End Sub

--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Gordon Bentley-Mix

SNAP!
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
G

Greg Maxey

Gordon,

Looks like we where on the same track about the same time ;-)

Joanne,

Rather than try to explain what you've done wrong, I'm just going to
show you the right way and let you work it out from there if that's
OK. If you have any questions just post back.

Sub PartnerInfo()
Dim arrNames() As String
Dim arrExtensions() As String
arrNames = Split("John Smith|Mike Love|William Jones", "|")
arrExtensions = Split("816|637|5102", "|")
If UBound(arrNames) <> UBound(arrExtensions) Then
MsgBox "Number of names not equal to number of extensions"
Exit Sub
Else
Dim arrPartnerList() As String
Dim i As Long
Dim n As Long
n = UBound(arrNames) 'or UBound(arrExtensions) - makes no real
difference
ReDim arrPartnerList(1, n) As String
For i = 0 To n
arrPartnerList(0, i) = arrNames(i)
arrPartnerList(1, i) = arrExtensions(i)
Next i
End If
End Sub

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
J

Joanne

Thank you both very, very much. Writing this code has been quite a learning
experience.
 
J

Joanne

Hello,
I tried the code and it skips the first name in the partner list and jumps
right to the second name. I wish I knew more about this to trouble shoot it.
I tried changing the numbers but it still always skips the first partner
name. I was wondering if you have any suggestions. Thanks very much for
your help.
 

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