VBA Application.Index with array causes type mismatch error 13

孤者浪人 提交于 2020-01-07 05:00:08

问题


My macro throws a type mismatch error when I use

Myarr= Application.Index(arr,0,1)

I have tried adding option explicit and defining the variables as variants but nothing seems to work.

The arr array is created from a CSV file and that contains 100000 rows and 11 columns.
The arr looks fine when I check it in the watch window (I can see the values for each row and column)

Here is the code:

Sub ArrTest()
    Dim Myarr 
    Dim Arr 
    Dim wb As Workbook 
    Set wb = Workbooks.Open("F:\People.csv")
    Arr = wb.Sheets(1).Range("A1").CurrentRegion.Value
    Myarr = Application.Index(Arr, 0, 2)
End Sub

Can anyone suggest what I am doing wrong?


回答1:


Many of the worksheet functions have a limit of just over 65k or so when it comes to the upper bound of input arrays, so you may be hitting that. Works for me with 65k, fails with 66k rows.

Sub ArrTest()
    Dim Myarr
    Dim Arr

    Arr = Range("a1:C65000").Value
    Myarr = Application.Index(Arr, 0, 1) '<<< OK

    Arr = Range("a1:C66000").Value
    Myarr = Application.Index(Arr, 0, 1) '<<<fails

End Sub

If you want to be able to handle more than 65k upper bound, then you will need to use a loop to populate your array "slice"



来源:https://stackoverflow.com/questions/28489253/vba-application-index-with-array-causes-type-mismatch-error-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!