Excel VBA中的多维多维数组 [英] redimension multidimensional arrays in Excel VBA

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

问题描述

看看下面的代码.我的问题是我不知道如何重新定义n整数和b整数.我正在做的是send1数组已经在工作,并且填充了大约4个句子.我需要仔细阅读每个句子并进行处理,但是遇到了麻烦.

Take a look at the following code. What my problem is is that I can't figure out how to redimension the n integer and the b integer. What I'm doing is the array sent1 is already working and it is populated with about 4 sentences. I need to go through each sentence and work on it but I'm having trouble.

dim sent1() 
dim sent2()

dim n as integer, b as integer, x as integer
dim temp_sent as string
b = 0
For n = 1 to ubound(sent1)

temp_sent = sent1(n)
    for x = 1 to len(temp_sent1)
    code
    if a then
        b = b + 1
        '**THIS IS THE PART OF THE CODE THAT IS NOT WORKING**
        redim preserve sent2(1 to ubound(sent1), b)
        sent2(n,b) = [code]
    next
next

推荐答案

您的代码中存在两个问题:

There are two issues in your code:

  1. Dim数组中未指定下限时,默认情况下它将基于0(除非已指定Option Base 1).明确指定后,下限可以是任何数字,而不仅仅是01
  2. 对于多维数组,Redim Preserve只能更改 last 维度,然后只能更改上限.
  1. When you Dim an array without specifying the lower bound it will by default be 0 based (unless you have specified Option Base 1). When explicitly specified, lower bound can be any number, not just 0 or 1
  2. For a multi dimensioned array, Redim Preserve can only change the last dimension, and then only the upper bound.

通常,我总是建议 指定下限和上限,例如

In general, I find it better to always specify Lower and Upper bounds, eg

Redim MyArray(1 to 10, 0 to 99)

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

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