VBA Public Array:如何? [英] VBA Public Array : how to?

查看:207
本文介绍了VBA Public Array:如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以今天的问题使我发疯,因为那应该很容易,而且我找不到答案:

So today's problem is getting me mad because that should be easy and i can not find the answer :

如何在VBA中声明公共数组?我正在使用字母A,B,C等组成的数组,因为我正在使用Excel单元格,并且我不想在我创建的每个函数中都声明它,对吗? 我尝试先在网络上浏览,然后阅读您必须在其他模块中声明它,这就是我所做的:

How to declare a public array in VBA ? I'm using an array with the letters A, B, C,... because i'm working with Excel cells, and i don't want to declare it in every function i create, right ? I've tried to look on the web first and i read that you have to declare it in a different module, so that's what i've done :

Public colHeader As String
colHeader = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L")

但是Visual Basic不喜欢它...

But Visual Basic doesn't like it...

那我该怎么办?

非常感谢:)

问题更多的是向数组赋值而不是对其进行声明

Edit : the problem is more about asigning values to the array than to declare it

推荐答案

在应用程序中的各个子实例之间将数组声明为全局数组:

Declare array as global across subs in a application:

Public GlobalArray(10) as String
GlobalArray = Array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L')

Sub DisplayArray()
    Dim i As Integer

    For i = 0 to UBound(GlobalArray, 1)
        MsgBox GlobalArray(i)

    Next i
End Sub

方法2:将数组传递给sub.使用ParamArray.

Method 2: Pass an array to sub. Use ParamArray.

Sub DisplayArray(Name As String, ParamArray Arr() As Variant)
    Dim i As Integer

    For i = 0 To UBound(Arr())
        MsgBox Name & ": " & Arr(i)
    Next i
End Sub

ParamArray必须是最后一个参数.

ParamArray must be the last parameter.

这篇关于VBA Public Array:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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