vba 如何用在module1 VBA中填充的数组填充列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19658626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 00:07:26  来源:igfitidea点击:

How to fill a listbox with an array filled in module1 VBA

arraysexcelvbaexcel-vbalistbox

提问by Mike

I'm new to creating user forms in VBA. I've been using VBA macros for a while now though so I understand some about them. Right now I'm creating a spreadsheet for a user where I'm using a user form with a list box. I have a lot of code in a regular module and need to refernce the module to fill the listbox. What I have right now is this:

我是在 VBA 中创建用户表单的新手。我已经使用 VBA 宏一段时间了,所以我对它们有所了解。现在我正在为一个用户创建一个电子表格,我在其中使用一个带有列表框的用户表单。我在常规模块中有很多代码,需要引用该模块来填充列表框。我现在所拥有的是:

Private Sub StartButton_Click()
 Dim Number
 Call GetTellerNames
 For Number = 0 To 40 Step 1
    If GetTellerNames(Number) <> "" Then
        ListBox1.AddItem (GetTellerNames(Number))
    End If
 Next
End Sub

When I run this I get an error saying

当我运行它时,我收到一条错误消息

Sub of Function not defined

子函数未定义

How do I fix this so that I can use the array in my module to fill the list box? I've already got the code to fill the array working.

如何解决这个问题,以便我可以使用模块中的数组来填充列表框?我已经有了填充数组工作的代码。

Here is the code for the GetTellerNames sub in the module:

这是模块中 GetTellerNames 子的代码:

Private Function GetTellerNames()
GetTellerNames = FindOthers(BranchNumber, TellerCode, 2)
End Function

It is using global variables that are set in other parts of the code. I can post all of the code if necessary.

它使用在代码的其他部分设置的全局变量。如有必要,我可以发布所有代码。

采纳答案by Mike

Since the GetTellerNames()code lies within a standard module you need to change the access modifier to publicin order to be able to access that method/sub from the UserForm1 object module.

由于GetTellerNames()代码位于标准模块中,因此您需要将访问修饰符更改为public,以便能够从 UserForm1 对象模块访问该方法/子。