2016-09-21 241 views
-1

我想聲明一個動態的字符串數組,然後調用其他每個過程。這是我的,我認爲它會工作,但我不斷收到錯誤。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 

回答

1

這裏有幾個問題。首先,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