gpt4 book ai didi

vba - ReDim Preserve 有什么作用?

转载 作者:行者123 更新时间:2023-12-01 17:21:55 28 4
gpt4 key购买 nike

我正在看别人的vba excel代码。他们在两个循环中都执行 ReDim Preserve dataMatrix(7, i) 。这是做什么的?

另外,第二个循环似乎只是覆盖了第一个循环中的数据,对吗?

Dim dataMatrix() As String

Worksheets.Item("ETS").Select
Do While Trim(Cells(r, 1)) <> ""
Debug.Print "The line: ", Trim(Cells(r, 1)), r
r = r + 1
dataMatrix(1, i) = Trim(Cells(r, 1)) ''file name
dataMatrix(2, i) = Trim(Cells(r, 2)) ''sample type
dataMatrix(3, i) = Trim(Cells(r, 3)) ''sample name
dataMatrix(4, i) = "ETS" ''
dataMatrix(5, i) = Trim(Cells(r, 5)) ''Response
dataMatrix(6, i) = Trim(Cells(r, 6)) ''ISTD Response
dataMatrix(7, i) = Trim(Cells(r, 10)) ''Calculated Conc
i = i + 1
ReDim Preserve dataMatrix(7, i)
Loop

r = 5
Worksheets.Item("ETG").Select
Do While Trim(Cells(r, 1)) <> ""
Debug.Print "The line: ", Trim(Cells(r, 1)), r
r = r + 1
dataMatrix(1, i) = Trim(Cells(r, 1)) ''file name
dataMatrix(2, i) = Trim(Cells(r, 2)) ''sample type
dataMatrix(3, i) = Trim(Cells(r, 3)) ''sample name
dataMatrix(4, i) = "ETG"
dataMatrix(5, i) = Trim(Cells(r, 5)) ''Response
dataMatrix(6, i) = Trim(Cells(r, 6)) ''ISTD Response
dataMatrix(7, i) = Trim(Cells(r, 10)) ''Calculated Conc
i = i + 1
ReDim Preserve dataMatrix(7, i)
Loop

最佳答案

Redim Preserve 允许您更改数组的维度,同时保留数组的内容。

每个循环末尾的 Redim Preserve 会向数组添加另一行。

我认为第二个循环正在附加到数组,因为 i 变量在循环之间没有更改。

关于vba - ReDim Preserve 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com