如何将Excel范围分配给2D数组? [英] How to assign an Excel Range to a 2D array?

查看:126
本文介绍了如何将Excel范围分配给2D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问你可以如何将Excel范围(G2:AA1000)分配给2D数组?如果可能的话,在2D阵列上执行一些操作后,如何将2D阵列返回相同的范围?将2D范围分配给2D阵列后,如何从该2D矩阵中识别每一行?



谢谢,

解决方案

有一个简单的方法来修改一个使用数组的区域,将其写入同一个地方或其他地方。



此示例代码将使用数组将数据从一个区域复制到另一个区域:

 子示例()
Dim testdata()
testdata = Range(A1:B13)
Range( D1:E13)= testdata'简单复制
范围(G1)= testdata'只复制1个单元格
范围(I1:K22)= testdata'尝试复制太多
End Sub

testdata 数组从 1 ,并将扩展到范围中指定的列和行数。在这种情况下, testdata(1,1)是指从 A1 testdata(1,2) 是指 B1 ,使用 testdata(13,1)指向 A13 testdata(13,2)引用 B13



设置与下一行中的数组相同的范围将数组复制到指定的位置。




  • 如果该区域小于原始数组,它将仅复制足够的数组来填充该空间,因此 Range(D1)= testdata 只会在表单上放置一个单元格。

  • 如果您指定较大的区域,则#N / A 将填充不在数组元素所涵盖的空间中的区域,因此 Range(A1:A3)= testdata 将使用数组填充A1和A2,但A3将具有#N / A



示例程序的结果:

注意:A1:B13是原始数据,随后的$ code> range(??)= testdata


Could you please say- how a Excel Range("G2:AA1000") can be assigned to a 2D array? If possible how to return back that 2D array to the same range after performing some operation on that 2D array?After assignment a Range to an 2D array,How each row will be identified from that 2D matrix?

Thanks,

解决方案

There is an easy way to make changes to an area using an array, and write it out to the same place, or somewhere else.

This example code will copy data from one area to another, using an array:

Sub example()
Dim testdata()
testdata = Range("A1:B13")
Range("D1:E13") = testdata ' simple copy
Range("G1") = testdata ' copy only 1 cell
Range("I1:K22") = testdata 'try to copy too much
End Sub

The testdata array starts from 1, and will extend to the number of columns and rows specified in the range. In this case, testdata(1,1) refers to the data obtained from A1, testdata(1,2) refers to B1, finishing up with testdata(13,1) referring to A13, and testdata(13,2) referring to B13.

Setting the range equal to the array in the next line copies the array into the specified location.

  • If the area is smaller than the original array, it will copy only enough of the array to fill that space, so Range("D1")=testdata will only place one cell on the sheet.
  • If you specify a larger area, then #N/A will fill the area that is not in the space covered by array elements, so Range("A1:A3")=testdata will fill A1 and A2 with data from the array, but A3 will have #N/A

Result of example program:
Note: A1:B13 is the original data, which gets copied with the subsequent range(??)=testdata

这篇关于如何将Excel范围分配给2D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆