如何从 Excel 电子表格中的列填充组合框? [英] How do I populate a combo box from a column in my excel spread sheet?

查看:40
本文介绍了如何从 Excel 电子表格中的列填充组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Excel 电子表格,一个有一个组合框,另一个有一个部门名称列表.我需要用部门名称填充组合框.我如何做到这一点.

I have two excel spreadsheets, one has a combobox, the other one has a list of department names. I need to populate the combobox with the department names. How do I acheive this.

推荐答案

这是一个 VBA 代码:

Here is a VBA Code:

Dim vArr as Variant
Dim i as Integer
vArr = WorksheetFunction.Transpose(Sheets(2).Range("A2:A10").value)
With Sheets(1).OLEObjects("ComboBox1").Object
     .Clear
     For i = Lbound(vArr) to Ubound(vArr)
        .AddItem vArr(i)
     Next i
End With

这是加载组合框的最简单方法,因为您的部门范围不会为空...

Here is the most simpler way to load the combobox, given your department range will not be empty...

Private Sub Workbook_Open()
    Sheets(1).ComboBox1.List = Sheets(2).Range("A2:A10").Value
End Sub

或在 Sheet1 中:

or within Sheet1:

Private Sub Worksheet_Activate()
    ComboBox1.List = Sheets(2).Range("A2:A10").Value
End Sub

这篇关于如何从 Excel 电子表格中的列填充组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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