简体   繁体   English

在VBA Excel中重用2D数组?

[英]Reusing 2D array in VBA Excel?

New here, and new to VBA and programming in general. 这里是新功能,也是VBA和一般编程的新功能。

I am trying to reuse multiple arrays (through a loop), but I assume I need to clear them out before I reuse them? 我试图(通过循环)重用多个数组,但是我想我需要在重用它们之前清除它们? I have tried searching through the questions, but I can't find a solution, or frankly can't understand the solutions if they work. 我曾尝试搜索所有问题,但找不到解决方案,或者坦白地说,如果解决方案无法解决,我将无法理解。

Dim WS As Worksheet

For Each WS In Worksheets
    If Right(WS.Name, 4) = "Data" Then Comp = Comp + 1
Next WS

Dim CompArray(10, 50) As Variant
Dim RatesArray(10, 1 To 50) As Variant
Dim IndexArray(10, 50) As Variant
Dim ShortIndexArray(10) As Long
Dim MRow, DRow, FRow, RRow, Col As Long
Dim LastCol, LastRow As Long

MRow = 4
LastRow = Cells(Rows.Count, 1).End(xlUp).Row


Do Until MRow > LastRow

    '**** MY CODE ****

''''***!!!*** Trying to Clear the array and reuse with the same dimensions for each loop ***!!!***
' not working

''Erase CompArray, RatesArray, IndexArray, ShortIndexArray
''ReDim CompArray(10, 50)
''ReDim RatesArray(10, 1 To 50)
''ReDim IndexArray(10, 50)
''ReDim ShortIndexArray(10)

MRow = MRow + 6 + Comp
Loop




End Sub

so when I go to the next step of the main loop, the arrays that I want to use have the same information. 因此,当我转到主循环的下一步时,我要使用的数组具有相同的信息。 I want to keep the same names and dimensions of the arrays, but just clear out the contents. 我想保持数组的名称和维数相同,但要清除内容。 It sounds simple to me, but I have no idea how to do this. 对我来说听起来很简单,但我不知道该怎么做。

I tried erasing and redim-ing (that was a solution to a similar problem on here) but it didnt work, it actually was saying I was declaring the arrays twice. 我尝试擦除和重新命名(这是此处类似问题的解决方案),但没有奏效,实际上是在说我两次声明了阵列。

I also tried dim-ing the arrays while already in the loop. 我也曾尝试在循环中使数组变暗。 That didn't work either. 那也不起作用。

Any help would be appreciated! 任何帮助,将不胜感激!

Thanks. 谢谢。

If you want to reuse an array then you shouldn't specify the dimensions when you first declare it 如果要重用数组,则在初次声明时不应该指定尺寸

Dim CompArray() As Variant

Then when you want to initialize it you use the keyword Redim 然后,当您要初始化它时,请使用关键字Redim

ReDim CompArray(10, 50)

If you redim your array again then you will loose any data already stored in it, unless you use the Preserve keyword 如果再次重新定义数组,则除非使用Preserve关键字,否则将丢失已经存储在其中的所有数据

ReDim Preserve CompArray(10, 60)

And if you're redimensioning your array and preserving the content then you can only change the last dimension 如果您要重新定义数组并保留内容,则只能更改最后一个维度

so ReDim Preserve CompArray(10, 80) will work as you're changing the last element. 因此ReDim Preserve CompArray(10, 80)将在您更改最后一个元素时起作用。 ReDim Preserve CompArray(100, 80) will cause an error as you're attempting to change the first element and preserve the data. 当您尝试更改第一个元素并保留数据时ReDim Preserve CompArray(100, 80)将导致错误。

ReDim CompArray(100, 800) will work fine, but you will lose all your currently stored data. ReDim CompArray(100, 800)可以正常工作,但是您将丢失所有当前存储的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM