当尝试使用变量访问2d数组值时,下标超出范围

时间:2016-11-10 15:35:46

标签: arrays excel vba excel-vba

我有一个工作簿,需要打开另一个工作簿,复制一个范围,然后将其粘贴到第一个工作簿中。目前,该范围正被复制到2D阵列中。在粘贴值之前,我需要检查该值是否为空,以便它不会将0粘贴到我的表中。之后,它应该将值粘贴到原始电子表格中并循环到下一个值。该数组需要是动态的,因为该函数将针对不同大小的表运行。列数将更改,但行数不变。

GetValue2函数将范围传递给myarray就好了,但当我尝试使用变量{{1}访问myarray中的单个值时,会出现“下标超出范围”错误}和ro。有关如何解决此问题的任何见解?请参阅下面的代码。

col

Public Function MultiColGetValsRev2(startj As Integer, starti As Integer, endj As Integer, endi As Integer, startc As Integer, startr As Integer, endc As Integer, endr As Integer, instcountrow As Integer, instcountcol As Integer, p As String, F As String, s As String, thisworksheet As String) Dim myarray() As Variant Dim colnum As Integer Dim rownum As Integer Dim ro As Integer Dim col As Integer Dim path, file, sheet As String Dim wkbk As Workbook path = p file = F sheet = s colnum = endc - startc rownum = 24 ro = 0 col = 0 ReDim myarray(rownum, colnum) 'getvalue2 call myarray = GetValue2(path, file, startr, endr, startc, endc) ReDim Preserve myarray(rownum, colnum) 'Paste values into flow map For j = startj To endj For i = starti To endi If col <= colnum Then If ro <= rownum Then 'Clear existing cell ThisWorkbook.Sheets(thisworksheet).Cells(i, j).Clear 'Check if cell is blank If myarray(ro, col) = isblank Then 'Error occurs here myarray(ro, col) = "" End If 'Paste value into flow map ThisWorkbook.Sheets(thisworksheet).Cells(i, j).Value = myarray(ro, col) End If ro = ro + 1 End If Next i col = col + 1 ro = 0 Next j End Function 功能如下:

GetValue2

0 个答案:

没有答案