Windows 開發人員中心

# Use the array

• ### 問題

• Hi,

The Sub Tombmuvelet() I want to use the elements of the tomb() array.
I would like to specify the value of the elements of the array Function Tombfeltolt() in the procedure.

Sub Tombmuvelet()
Dim tomb(7)
Dim sz As Integer
Call Tombfeltolt
sz = tomb(1)
sz1=tomb(2)
sz2=tomb(3)
Range("A1").Select
End Sub
Function Tombfeltolt()
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i

End Function

Best regards,

Józsi

2019年12月7日 上午 05:42

### 解答

• Instead of using a function, you can pass the array to Tombfeltolt:

```Sub Tombmuvelet()
Dim tomb(6)
Dim sz As Integer, sz1 As Integer, sz2 As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
End Sub

Sub Tombfeltolt(tomb)
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Sub```

Regards, Hans Vogelaar (http://www.eileenslounge.com)

• 已標示為解答 2019年12月7日 上午 11:20
2019年12月7日 上午 10:25
• Hi,

I suppose you need to define tomb in Function Tombfeltolt, otherwise you need to
1) pass over tomb as an argument of Function Tombfeltolt or
2) define tomb(7) outside of Sub Tombmuvelet

1) argument or parameter
```Option ExplicitSub Tombmuvelet()
Dim tomb(7) As Integer
Dim sz As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
Range("A1").Select
End Sub

Function Tombfeltolt(ByRef tomb() As Integer)
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Function```
2) variable outside of sub
```Option Explicit

Dim tomb(7) As Integer    ' --<< outside of sub

Sub Tombmuvelet()
Dim sz As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
Range("A1").Select
End Sub

Function Tombfeltolt()
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Function```
Regards,

Ashidacchi -- http://hokusosha.com

• 已編輯 2019年12月7日 上午 10:31
• 已標示為解答 2019年12月7日 上午 11:20
2019年12月7日 上午 10:30

### 所有回覆

• Instead of using a function, you can pass the array to Tombfeltolt:

```Sub Tombmuvelet()
Dim tomb(6)
Dim sz As Integer, sz1 As Integer, sz2 As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
End Sub

Sub Tombfeltolt(tomb)
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Sub```

Regards, Hans Vogelaar (http://www.eileenslounge.com)

• 已標示為解答 2019年12月7日 上午 11:20
2019年12月7日 上午 10:25
• Hi,

I suppose you need to define tomb in Function Tombfeltolt, otherwise you need to
1) pass over tomb as an argument of Function Tombfeltolt or
2) define tomb(7) outside of Sub Tombmuvelet

1) argument or parameter
```Option ExplicitSub Tombmuvelet()
Dim tomb(7) As Integer
Dim sz As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
Range("A1").Select
End Sub

Function Tombfeltolt(ByRef tomb() As Integer)
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Function```
2) variable outside of sub
```Option Explicit

Dim tomb(7) As Integer    ' --<< outside of sub

Sub Tombmuvelet()
Dim sz As Integer
Call Tombfeltolt(tomb)
sz = tomb(1)
sz1 = tomb(2)
sz2 = tomb(3)
Range("A1").Select
End Sub

Function Tombfeltolt()
Dim i As Integer
For i = 0 To 6
tomb(i) = i + 2
Next i
End Function```
Regards,

Ashidacchi -- http://hokusosha.com

• 已編輯 2019年12月7日 上午 10:31
• 已標示為解答 2019年12月7日 上午 11:20
2019年12月7日 上午 10:30
• Hi,

The second varieble prints an error message.

Best regards,

Józsi

2019年12月7日 上午 11:29
• Hi Józsi,

Sorry, I provide both code without executing it (only an idea on paper).
How about the first code? Did it work with no errors?

Regards,

Ashidacchi -- http://hokusosha.com

2019年12月7日 上午 11:35