arrays - 在 Visual Basic 编辑器中声明全局数组

标签 arrays vba

似乎没有其他答案对我有用,所以我只好提出一个每个人都认为有问题的问题。除 VBA 之外的任何其他语言上的简单内容。我只想初始化一个全局字符串数组,并在我的主子中使用它。

这是 test1,我只是尝试从公共(public)函数返回它:

Public Function effthis1() As String()
    ReDim effthis1(0 To 10)
    myStr = "a b c d e f g h i j k"
    strsplit = Split(myStr)

    j = LBound(effthis)
    For Each word In strsplit
        effthis1(j) = word
        j = j + 1
    Next

End Function

Sub test1()
    testStr = effthis1(4)
    MsgBox testStr
End Sub

这是 test2,我尝试使用在主子程序中调用的子程序:

Public effthis2() As String

Sub declareMyArray()

effthis2(0) = "a"
effthis2(1) = "b"
effthis2(2) = "c"
effthis2(3) = "d"
effthis2(4) = "e"
effthis2(5) = "f"
effthis2(6) = "g"
effthis2(7) = "h"
effthis2(8) = "i"
effthis2(9) = "j"
effthis2(10) = "k"

End Sub

Sub test2()
    declareMyArray
    MsgBox effthis2(4)
End Sub

MSDN 根本没有帮助。提前致谢,乔治

最佳答案

在第一个示例中,您必须声明变量,然后无需运行循环即可获取该字符串。

Public Function effthis1(j As Integer) As String
    Dim strsplit() As String
    myStr = "a b c d e f g h i j k"
    strsplit = Split(myStr)

    effthis1 = strsplit(j)

End Function

Sub test1()
    testStr = effthis1(4)
    MsgBox testStr
End Sub

编辑:

根据您的评论,teststr 必须是一个数组,您可以将整个数组加载到其中:

Public Function effthis1() As String()

    myStr = "a b c d e f g h i j k"
    effthis1 = Split(myStr)

End Function

Sub test1()

    teststr = effthis1
    MsgBox teststr(4)
End Sub

关于arrays - 在 Visual Basic 编辑器中声明全局数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32952113/

相关文章:

excel - 循环优化/定制

vba - 不活动后触发代码

C数组声明和赋值?

c - 我如何找出一个值是否在 C 的数组中?

php - 以查询形式回显数组值

用于循环浏览从特定工作表(索引 3)开始的工作表的 VBA 代码

vba - 如何阻止Excel中的图表自动更新

excel - VBA检查文件是否打开

jquery - 如何更新本地存储阵列的特定键值?

javascript - 从javascript中的数组数组中获取唯一的日期时间字符串值