Word中的VBA数组
问题描述:
我想声明一个动态的字符串数组,然后调用其他每个过程。这是我的,我认为它会工作,但我不断收到错误。Word中的VBA数组
Option Explicit
Sub Main()
Public ListArr() As String
Application.Run "Get_Input_List"
End Sub
Function Get_Input_List(ByRef ListArr() As String) As String
Dim list As String
Dim MesBox As String
list = InputBox("Please enters word to sort using a comma with no space to separate", "Words")
ListArr = Split(list, ",")
MsgBox ListArr(1)
End Function
答
这里有几个问题。首先,Public
只能用作模块级别变量的访问修饰符 - 不是本地的,所以它应该是Dim ListArr() As String
。其次,您不需要使用Application.Run
来调用同一个项目中的过程。如果确实是,那么您没有传递所需的参数(无论如何都不能通过Application.Run
传递ByRef
)。第三,Get_Input_List
是一个函数,所以你应该返回Split
的结果。现在它总是返回vbNullString
。我猜你正在寻找更像这样的东西:
Option Explicit
Sub Main()
Dim ListArr() As String
ListArr = Get_Input_List
Dim inputWord As Variant
For Each inputWord In ListArr
MsgBox inputWord
Next
End Sub
Function Get_Input_List() As String()
Dim list As String
list = InputBox("Please enter words to sort separated with a comma and no spaces", "Words")
Get_Input_List = Split(list, ",")
End Function