数组程序中的语法错误 [英] Syntax error in array program
问题描述
我正在尝试使用数组方法将工作表的数据范围从一个地方写到另一个地方,以期稍后尝试进行数组操作.该程序运行正常.
I am trying to write worksheet data range from one place to other using array approach with a view to try array manipulation later. This program works fine.
Sub Array2Range5()
Dim Directory As Variant
Directory = Range("A30:D39").Value2
For i = LBound(Directory, 1) To UBound(Directory, 1)
For j = LBound(Directory, 2) To UBound(Directory, 2)
Worksheets("List").Range(Worksheets("List").Cells(i + 1, j + 1), Worksheets("List").Cells(i + 1, j + 1)) = Directory(i, j)
Next j
Next i
End Sub
出于相同目的的以下程序给出运行时错误"9".下标超出了以下代码部分的范围.
The following program for same purpose gives run time error ‘9’.Subscript out of range in the following code portion.
Worksheets("List").Range(Worksheets("List").Cells(i + 1, j + 1), Worksheets("List").Cells(i + 1, j + 1)) = Directory(i, j)
我无法更正它,程序如下.
I am not able to correct it and the program is as follows.
Sub Array2Range6()
Dim Directory As Variant
Directory = Range("A30:D39").Value2
For i = 0 To UBound(Directory, 1)
For j = 0 To UBound(Directory, 2)
Worksheets("List").Range(Worksheets("List").Cells(i + 1, j + 1), Worksheets("List").Cells(i + 1, j + 1)) = Directory(i, j)
Next j
Next i
End Sub
我的查询是这些行的语法是否错误.如果是这样,相似行上的正确语法应该是什么
My query is whether these lines are wrong in syntax. If so what should be the correct syntax on similar lines
For i = 0 To UBound(Directory, 1)
For j = 0 To UBound(Directory, 2)
任何帮助都将受到高度赞赏.
Any help shall be highly appreciated.
推荐答案
将工作表范围中的值转储到数组中始终会导致基于1的2D数组而不是基于0的1D或2D数组
Dumping values from a worksheet range into an array always results in a 1-based 2-D array not a zero-based 1-D or 2-D array.
对此进行以下证明:
Dim Directory As Variant
Directory = Range("A30:D39").Value2
debug.print LBound(Directory, 1) & " to " & UBound(Directory, 1)
debug.print LBound(Directory, 2) & " to " & UBound(Directory, 2)
第一个有效,因为您正在使用LBound;后者失败,因为您将LBound实际为1时指定为零.
The first works because you are using the LBound; the latter fails because you are specifying the LBound to be zero when it is actually 1.
就个人而言,无论数组是基于零的一维数组还是基于一维的一维数组,我总是使用LBound作为下边界(使用
Personally, I always use LBound for the lower boundary regardless of whether the array is a zero-based 1-D array, a one-based 1-D array (using Option Base 1 directive) or a one-based 2-D array.
这篇关于数组程序中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!