2017-07-31 158 views
0
If IsArray(payCsv(pay_id)) = False Then 
    'create tempArray 
    lc = 0 
    Debug.Print "create array" 
End If 

If IsArray(payCsv(pay_id)) = True Then 
    Debug.Print " array exists, we should be able to get ubound" 
    lc = UBound(payCsv(0)) - LBound(payCsv(0)) 
    l = l + 1 
End If 

我使用上面的代碼,以確定我是否可以在我的二維數組上使用UBOUND(即,如果創建了第二維,獲取長度(UBOUND -VBA編譯錯誤內if語句

LBOUND)。但是,我得到一個編譯錯誤,即使條件2是錯誤的,它不會識別代碼將不相關。

我正在測試一個數組,結果是如果我註釋掉「lc = UBound payCsv(0)) - LBound(payCsv(0))「是」create array「。

如果我離開這個在那裏,我得到錯誤「編譯錯誤 - 預期數組」

這是VBA中的錯誤?

回答

2

如果要訪問數組的第二維的UBound函數,格式是這樣的:

UBound(payCSV, 2) 

MSDN page這個功能可能會有所幫助。

當您像當前那樣訪問payCSV(0)時,代碼假定您想要payCSV數組第一維內的第一個元素。

也許你可能想試試這個?

If IsArray(payCsv(pay_id)) = False Then 
    'create tempArray 
    lc = 0 
    Debug.Print "create array" 
Else 
    Debug.Print " array exists, we should be able to get ubound" 
    lc = UBound(payCsv, 2) - LBound(payCsv, 2) 
    l = l + 1 
End If 
+0

OMG ...不敢相信我錯過了那個..我會盡我所能接受答案。 – David