|
如何定义动态数组?
例子如下:在第一行单元格中有如下8个数
我想找有多少个不是0的数,然后组成数组,请问该怎么办?
dim arr(1 to 8)
j=1
for i=1 to 8
if cells(1,i)<>0 then
arr(j)=cells(1,i)
j=j+1
end if
next i
dim arr1(1 to j)
for m=1 to j
if arr(m)="" then
exit for
arr1(m)=arr(m)
next m
如上的代码,最终得到arr1,
我感觉上面的代码有点繁琐,肯定不是好程序,所以应该怎么编写呢?
Dim arr()
For i = 1 To 8
If Cells(1, i) <> 0 Then
j = j + 1
ReDim Preserve arr(j)
arr(j - 1) = Cells(1, i)
End If
Next i
ReDim Preserve arr(UBound(arr) - 1)
数组arr就是你想要的结果
|
|