Using a Two Dimensional Array

Now that you know how to programmatically produce a list (a one-dimensional array), it's time to take a closer look at how you can work with tables of data. The following procedure creates a two-dimensional array that will hold the country name, currency name, and exchange rate for three countries.

Sub Exchange()

Dim t As String Dim r As String Dim Ex(3, 3) As Variant

Ex(1, 2) = "Yen" Ex(1, 3) = 128.2 Ex(2, 1) = "Mexico" Ex(2, 2) = "Peso" Ex(2, 3) = 9.423 Ex(3, 1) = "Canada" Ex(3, 2) = "Dollar" Ex(3, 3) = 1.567

MsgBox "Country " & t & t & "Currency" & t & "per US$" _ & r & r _

& Ex(1, 1) & t & t & Ex(1, 2) & t & Ex(1, 3) & r _ & Ex(2, 1) & t & t & Ex(2, 2) & t & Ex(2, 3) & r _ & Ex(3, 1) & t & t & Ex(3, 2) & t & Ex(3, 3), , _ "Exchange"

End Sub

When you run the Exchange procedure, you will see a message box with the information presented in three columns (Figure 7-2).

Exchange

E3

Country

Currency uerUSÎ

■Japan

Yen 128.2

Mexico

Pesa 9:423.

Canada

Dollar 1.5G7

OK I

The text displayed in a message box can be custom formatted.

0 0

Post a comment