VBA多维数组 [英] VBA Multidimensional Arrays

查看:1408
本文介绍了VBA多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法做到在VBA以下?

Is there a way to do the following in VBA?

初​​始化多维数组,并用一系列的数字填满它,

Initialize a multidimensional array and fill it with a series of numbers,

 1  2  3  4  5 
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20

然后删除某些特定列,对于如。 ,2栏1,4。因此,最终结果是公正的第3和第5列

Then remove some specific columns, for eg. columns 1, 2, 4. So that the end result is just the 3rd and 5th column.

最后一个人如何最终输出转换为正常一维阵列

Lastly how does one convert the final output to a normal one dimensional array.

推荐答案

假设你在Excel sheeet有这些列。如果仅在这些列有这些数据你可能会简单的删除你需要的列:D然后,你将结束与你的愿望2 columsn。不知道你真正需要在年底这是最好的盲目猜测。

Assume that you have these columns in an Excel sheeet. If you only have these data in these columns you may simple delete the columns you need :D Then you will end up with 2 columsn you desire. Without knowing what you really need at the end this is the best blind guess..

例如。您在列B开始到F:

e.g. your columns starts at B to F:

Columns("B:B").Delete Shift:=xlToLeft
Columns("C:C").Delete Shift:=xlToLeft
Columns("D:D").Delete Shift:=xlToLeft

您可以使用相同的逻辑来处理数组。

You can use the same logic to process the array.


  • 转置数组的表。

  • 删除列。

  • 然后向左转了两列数组。

但你会在最后两列离不开将其放入表?很好奇。所以,请确认您所需要的,所以在这里任何人都可以帮你。

But what will you do with the last two columns without putting it into the sheet? Very curious. So please confirm what you need, so anyone here can help you.

编辑按OP的评论:

您可以看看这个帖子和文章有不同的方式阵列manupulation:

You may take a look at this posts and articles which has manupulation of arrays in different ways:

  • Excel clear cells based on contents of a list in another sheet
  • VBA Arrays

然后,为了填充一个二维数组在VBA的例子,检查了这一点:

Then in order to populate a 2D array for an example in VBA, check this out:

Dim i As Integer, j As Integer
Dim array2D As Variant, newArray2D as Variant
'-- 0 indexed based array with 2 rows 3 columns
ReDim array2D(0 To 1, 0 To 2)

For i = LBound(array2D, 1) To UBound(array2D, 1)
    For j = LBound(array2D, 1) To UBound(array2D, 1)
        array2D(i, j) = i + j
    Next j
Next i

'--to delete the fastest is to use the above logic (worksheet)
'-- here you don't need to declare/redimentioned the array
'-- as transpose will do it with a 1 indexed based array

newArray2D = WorksheetFunction.Transpose(Sheets(2).Range("B2:D").Value)

这篇关于VBA多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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