Microsoft Visual Basic:如何初始化数组变量? [英] Microsoft Visual Basic: how to initialize an array variable?

查看:119
本文介绍了Microsoft Visual Basic:如何初始化数组变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个宏,它对文档中一组特定的字词样式的每个实例执行某些操作.

I wrote a macro which performs certain operations on every instance of a specific set of Word Styles within a document.

为此,我通过以下方式创建了一个名称数组:

To do so, I created an array of names this way:

Dim mylist(4) As String
mylist(1) = "Heading 1" 
mylist(2) = "Heading 2" 
mylist(3) = "Heading 3" 
mylist(4) = "Caption"

我找不到任何帮助页面(在Office帮助中或在microsoft.com内),其中提到了这样做的一种较短方法.有什么语法可以让我将其简化为(pseudocode)

I was unable to find any help pages (inside Office Help or at microsoft.com) which mentioned a shorter way to do so. Is there any syntax that would let me simplify this into something like (pseudocode)

mylist(1:4) = ["Heading 1", "Heading 2", "Heading 3", "Caption"]

我正在寻找一种单行加载数组的通用解决方案,无论是字符串还是数字,当我不希望使用诸如 all 这样的样式的整个集合时,一个文件.

I'm looking for a general solution for one-line loading of an array, whether it's strings or numbers, when I don't want the entire collection of something like, say all styles in a document.

我遇到了 Visual Basic中的集合初始化语法2008年?,这表明答案是直到VB10才开始".欢迎对该结论进行任何更新.

I ran across Collection initialization syntax in Visual Basic 2008?, which suggests the answer is "not until VB10" . Any updates to that conclusion would be welcome.

推荐答案

这很接近,但与以下内容略有不同:Dim mylist(4)As String

This is close but a little different than: Dim mylist(4) As String

Dim myarray As Variant
myarray = Array("Cat", "Dog", "Rabbit")

来自: http://www. mrexcel.com/forum/excel-questions/18225-initializing-arrays-single-statement.html

这篇关于Microsoft Visual Basic:如何初始化数组变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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