使用另一个子数组

时间:2019-06-13 11:08:59

标签: excel vba

我有一个包含几个数组的子(initialisation)。然后,我要创建另一个子(testSub),该子必须访问initialisation中的一个数组。我不确定如何执行此操作,将不胜感激。

初始化子项:

Sub initialisation()

   init_array = Array("apple", "orange", "car")

   init_array_2 = Array("coconut", "keys", "blue")

End Sub

testSub:

Sub testSub()

For Each element in init_array 'Does not work currently
   [do stuff]
Next

End Sub

2 个答案:

答案 0 :(得分:4)

您需要像这样将其作为参数传递:

Option Explicit
Sub initialisation()

    Dim init_array As Variant, init_array_2 As Variant

    init_array = Array("apple", "orange", "car")
    init_array_2 = Array("coconut", "keys", "blue")
    testSub init_array

End Sub
Sub testSub(init_array As Variant)

    Dim element As Variant

    For Each element In init_array 'Does not work currently
       [do stuff]
    Next

End Sub

您还应该使用Option Explicit,这将迫使您声明所有变量。

答案 1 :(得分:2)

您还可以在子对象之外定义数组:

PLAY [web] *******************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
ok: [34.242.219.28]

TASK [Verify python dependencies for aws s3] *********************************************************************
[DEPRECATION WARNING]: Invoking "pip" only once while using a loop via squash_actions is deprecated. Instead of 
using a loop to supply multiple items and specifying `name: "{{ item }}"`, please use `name: ['boto', 'boto3', 
'botocore']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be 
disabled by setting deprecation_warnings=False in ansible.cfg.
changed: [34.242.219.28] => (item=[u'boto', u'boto3', u'botocore'])

TASK [basic upload] **********************************************************************************************
ok: [34.242.219.28]

PLAY RECAP *******************************************************************************************************
34.242.219.28              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0