转载自:http://www.dailydoseofexcel.com/archives/2004/06/28/redim-an-array/

Array variables can be static or dynamic. That’s determined by the Dim statement. If you specify dimensions when you declare the variable, it’s static and always will be. If you leave the dimensions blank in the Dim statement, it’s dynamic and can be changed.

Dim Array1(1 To 10) As String ‘static array
Dim Array2() As String ‘dynamic array

Dynamic arrays can be changed using the Redim statement.

Dim Arr1() As Double

ReDim Arr1(Selection.Columns.Count, Selection.Rows.Count)

If you use Redim, all the data in your array is lost, unless you use the Preserve keyword. This keeps the data in tact, but limits what you can change with a Redim. For instance, when you use Preserve, you can only change the last dimension of the array. Sometimes you have to organize your array horizontally to accomodate this restriction.

Dim Arr1() As Double
Dim cell As Range
Dim i As Long

For Each cell In Range(“A1:A100?).Cells
    If cell.Value < 0.5 Then
        i = i + 1
        ReDim Preserve Arr1(1 To 2, 1 To i)
        Arr1(1, i) = cell.Value
        Arr1(2, i) = cell.Row
    End If
Next cell

Preserve is an expensive keyword, so you use it sparingly. Many people will Redim their arrays in blocks to avoid having to do it in every iteration of a loop.

ReDim Preserve Arr1(1 To 2, 1 To 10)

For Each cell In Range(“A1:A100?).Cells
    If cell.Value < 0.5 Then
        i = i + 1
        If i Mod 10 = 0 Then
            ReDim Preserve Arr1(1 To 2, 1 To i + 10)
        End If
        Arr1(1, i) = cell.Value
        Arr1(2, i) = cell.Row
    End If
Next cell

I’m not a big fan of the block Redim, but if you have a really time intensive procedure and this shaves some valuable milliseconds, then go for it. If there’s a way to figure out the upper bounds of the array before you add data, then you may save time there also.

Dim SmallCells As Long

SmallCells = Application.Evaluate(“=sumproduct(–(a1:A100<.5))”)

ReDim Arr1(1 To 2, 1 To SmallCells)

For Each cell In Range(“A1:A100?).Cells
    If cell.Value < 0.5 Then
        i = i + 1
        Arr1(1, i) = cell.Value
        Arr1(2, i) = cell.Row
    End If
Next cell

VBA - Redim an Array相关推荐

  1. VBA中数组(Array)与随机数(Rnd)的使用

    作者: 奔跑的犀牛先生 本文链接:https://blog.csdn.net/xuemanqianshan/article/details/88962097 一 数组 array 1.1 数据定义 静 ...

  2. VBA 开发学习--基础语法

    MsgBox "开始学习VBA" '提示框Dim str As String '声明str变量是string类型 Let str = "一起来看流星雨" '给变 ...

  3. QT_OpenGL渲染总结

    Qt 5的图形架构非常依赖OpenGL作为底层3D图形API,但近年来,随着Metal和Vulkan的推出,Qt 6完全改变了局面.Qt Quick中的所有3D图形现在都建立在新的3D图形抽象层之上, ...

  4. 检测打印机状态(VB实现)

    没有时间翻译,各位见谅啊?代码很简单,注意API的使用! Check this out... I found this while surfing internet from Spanish site ...

  5. 老男孩上海校区Python面试题

    python面试题 第一章:python基础 数据类型: 1 字典: 1.1 现有字典 dict={'a':24,'g':52,'i':12,'k':33}请按字典中的 value 值进行排序? 1. ...

  6. VBA每日一练(18),数组array的用法 redim preserve等

    定义方式 dim  arr11 (5) dim  arr12 (0 to 5 ) dim  arr13 (1 to 5) dim arr21(1,5) dim arr22(0 to 1 ,0 to 5 ...

  7. VBA 为什么你redim() 动态二维数组总出错?因为 redim 动态数组不太适合和循环搭配

    1总结 1.1 如何避免出错 如果是想生成一个二维数组,最后用静态声明,或则二次声明redim 一个足够大的动态数组 1.2 出错的原因是什么? redim时只有最后1维可变化,redim不太适合和循 ...

  8. VBA关于数组Dim,ReDim Preserve运行速度对比思考

    VBA关于数组Dim,ReDim Preserve运行速度对比思考 如何插入一段漂亮的代码片 以前看教程经常有人说ReDim Preserve动态定义数组运行速度非常慢,特做测试分析是否真的想网友说的 ...

  9. VBA 在循环里多次redim动态数组一定注意,需要加preserve

    1  错误例子 For i = 1 To count_newRandomizep2 = Int(1 + 10 * Rnd)Debug.Print "第" & i & ...

最新文章

  1. java杂记-static
  2. java web项目使用log4j的使用笔记
  3. 手机端html表格,jQuery Mobile 表格
  4. python最新面试题_2018年最新Python面试题及答案
  5. cmd长ping记录日志和时间_四个网络命令ping、arp、tracert、route的详细用法
  6. 三元表达式,递归,匿名函数,内置函数
  7. tf调不到keras怎么 回事_格力变频空调快速维修方法及技巧 空调压缩机不到一分钟就停,怎么回事?...
  8. java hibernate方言_java – 如何在运行时获取Hibernate方言
  9. 化作春泥更护花的上一句是什么?化作春泥更护花作者是谁
  10. TCP连接吞吐率和线路效率的总结
  11. 太网交换机芯片-KSZ系列
  12. FME入门视频教程:第三节 FME界面及常用设置
  13. jdk中运行java程序的工具,采用JDK工具编译运行java程序
  14. 未来教育python有错吗_未来教育题库错误大列举,建议不要用它的题库复习了
  15. windows平台服务监控邮件报警批处理脚本
  16. ASP.NET Core 认证与授权[3]:OAuth OpenID Connect认证
  17. ACPI 待机/睡眠/休眠有啥区别?
  18. 实时语音如何过质量关?
  19. 傅里叶变换、拉普拉斯变换与z变换对比
  20. MongoDB Project(投影字段)

热门文章

  1. 一分钟让你了解什么是ELP
  2. 2018年辽宁省电子设计大赛D题手势识别装置
  3. Gingko Framework(struts集成ibatis)
  4. win10电脑怎样扩大C盘空间
  5. 解决新建springboot项目时包导不进来的问题sun.security.provider.certpath.SunCertPathBuilderException: unable to f,已解决
  6. 2020年flag立起来
  7. 程序人生:初学者中最最最常问的问题都有哪些呢???
  8. 利用群晖nas备份华为手机数据
  9. vivoS7e和vivoS6哪个好(参数对比还是新机优势大)
  10. android时间格式am pm,将字符串在12(PM / AM)小时AM PM时间转换为24小时时间android