ReDim Preserve做什么? [英] What does ReDim Preserve do?

查看:338
本文介绍了ReDim Preserve做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



另外,似乎第二个循环只是覆盖第一个循环中的数据,是否正确?

  Dim dataMatrix()As String 

Worksheets.Item(ETS)。选择
尽管修剪(细胞(r ,1))<
Debug.Print行:,Trim(Cells(r,1)),r
r = r + 1
dataMatrix(1,i)= Trim(Cells ,1))''文件名
dataMatrix(2,i)= Trim(Cells(r,2))''sample type
dataMatrix(3,i)= Trim(Cells(r,3) )''样本名称
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保存dataMatrix(7,i)
循环

r = 5
Worksheets.Item(ETG)。选择
Do While Trim细胞(r,1))
Debug.Print行:,Trim(Cells(r,1)),r
r = r + 1
dataMatrix(1,i)= Trim(Cells ,1))''文件名
dataMatrix(2,i)= Trim(Cells(r,2))''sample type
dataMatrix(3,i)= Trim(Cells(r,3) )''样本名称
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保存dataMatrix(7,i)
循环


解决方案

ReDim 在修改数组的大小时保留数组中的数据。此外,它不应该覆盖数据因为 i 在循环的每次迭代都递增:第一个循环将不会停止,直到它遇到一个空单元格,每次迭代更改数组长度一个,所以当lenght是 8 时,它将添加到 i ,新长度变为 9 。当第二次迭代发生时,将新元素写入索引 9 的数组的边界,再次修改其长度,使其成为 1 元素更长,并且迭代直到遇到空行。


I am looking at someone else's vba excel code. they are doing ReDim Preserve dataMatrix(7, i) in both loops. What does this do?

Also, it seems like the second loop just overwrites the data in the first loop, is that correct?

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 preservers a data in an array when you modify its size. Also it shouldn't overwrite the data becasuse i is incremented every iteration of the loop: the first loop won't stop until it encounters an empty cell, changing array length every iteration by one, so that when the lenght is 8 it adds 1 to the i and the new length becomes 9. When the second iteration occurs it writes the new element to the bound of the array with index 9, modifies its length again so that it becomes 1 element longer and iterates until encounters an empty line.

这篇关于ReDim Preserve做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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