vba 列表框多列添加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11213962/
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-08 13:24:01  来源:igfitidea点击:

vba listbox multicolumn add

vba

提问by Jignesh Makwana

Possible Duplicate:
Adding items in a Listbox with multiple columns

可能的重复:
在具有多列的列表框中添加项目

With MFC VC++ there are two controls, ListBoxand ListCtrl. But with VBA it seems we have only ListBox.

MFC VC++ 有两个控件,ListBoxListCtrl. 但是使用 VBA 似乎我们只有ListBox.

I want to create a listbox with 2 columns (Company_ID, Company_Name).

我想创建一个包含 2 列(Company_ID、Company_Name)的列表框。

Here is what I tried:

这是我尝试过的:

  1. I created lstbox(control type ListBox)
  2. Row source type = value list
  3. I am taking value from user from two edit boxes and when user clicks "add" then it should be added to the listbox with 2 columns.
  1. 我创建了 lstbox(控件类型 ListBox)
  2. 行源类型 = 值列表
  3. 我从两个编辑框中获取用户的价值,当用户单击“添加”时,它应该添加到具有 2 列的列表框。

In the VBA code routine I added the following lines:

在 VBA 代码例程中,我添加了以下几行:

lstbox.ColumnCount = 2
lstbox.AddItem (Company_ID)

The following code is not working which seems to be related with adding column value:

以下代码不起作用,这似乎与添加列值有关:

lstbox.Column(1,lstbox.ListCount - 1) = Company_name

This gives error:

这给出了错误:

Runtime error '424' object required.

需要运行时错误“424”对象。

Could anyone help with vba code to add to multi column listbox.

任何人都可以帮助使用 vba 代码添加到多列列表框。

回答by Trace

Simplified example (with counter):

简化示例(带计数器):

With Me.lstbox
    .ColumnCount = 2
    .ColumnWidths = "60;60"
    .AddItem
    .List(i, 0) = Company_ID
    .List(i, 1) = Company_name 
    i = i + 1

end with

Make sure to start the counter with 0, not 1 to fill up a listbox.

确保以0开始计数器,而不是 1 来填充列表框。