How to pass a 2D array by reference?

Pastor

New Member
Joined
Jun 23, 2010
Messages
20
'I need to know the correct syntax for passing
'BYREF a 2 dimentional array defined with a
'TYPE to a subroutine. I need to pass it by
'reference rather than define it as public.
'Thanks.
'
Code:
Option Explicit
Type Information
    sSex As String
    sName  As String
    sAddress As String
End Type
Private Sub Initialize_Array()
Dim aiPeople(2, 2) As Information
aiPeople(1, 1).sSex = "Male"
aiPeople(1, 1).sName = "John"
aiPeople(1, 1).sAddress = "Apartment 101"
'This next line does not work;
'The error message that I get is:
' "ByRef type argument mismatch"
Display_Information ((aiPeople))
End Sub
Private Sub Display_Information(ByRef aiCouple As Information)
Dim iRow As Integer
Dim iCol As Integer
For iRow = 1 To 2
    For iCol = 1 To 2
        MsgBox aiCouple(iRow, iCol)
    Next J
Next I
End Sub
'
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this:
Rich (BB code):

Option Explicit
Type Information
  sSex As String
  sName As String
  sAddress As String
End Type

Private Sub Initialize_Array()
  Dim aiPeople(1 To 2, 1 To 2) As Information
  aiPeople(1, 1).sSex = "Male"
  aiPeople(1, 1).sName = "John"
  aiPeople(1, 1).sAddress = "Apartment 101"
  aiPeople(2, 1).sSex = "Female"
  aiPeople(2, 1).sName = "Helen"
  aiPeople(2, 1).sAddress = "Apartment 100"
  Display_Information aiPeople
End Sub

Private Sub Display_Information(ByRef aiCouple() As Information)
  Dim iRow As Long
  Dim iCol As Long
  For iRow = 1 To 2
    For iCol = 1 To 2
      With aiCouple(iRow, iCol)
        MsgBox .sSex & vbLf & .sName & vbLf & .sAddress
      End With
    Next
  Next
End Sub
 
Upvote 0
Thank You Vladimir!

I'll say a pray for you that God will bless you for your kindness to me.


God Bless.
 
Upvote 0
Thank You Vladimir!

I'll say a pray for you that God will bless you for your kindness to me.


God Bless.
Good to know that it helped, thank you for feedback and warm words!
Kind Regards,
Vlad
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,719
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top