VBA如何通过名称访问列表框excel-2010 [英] VBA How to access a listbox by name excel-2010

查看:90
本文介绍了VBA如何通过名称访问列表框excel-2010的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的VBA代码,用于从列表框中获取选定的项目.我想将列表框的名称作为字符串发送,然后以这种方式访问​​列表框,因为我无法直接访问列表框,因为此代码与工作表源代码位于单独的模块中.

Below is my VBA code to get a selected item from a listbox. I want to send the name of the listbox as a string then access the list box that way since I can't access the listbox directly because this code is in a seperate module from my worksheet source code.

所以我要问的是如何通过字符串访问列表框. 例如,代替

So what I am asking is how do you access a listbox by string. For example, instead of

Worksheets("sheet1").ListBox1

我想要

Worksheets("sheet1")."ListBox1"

因此,只要我具有列表框的名称,我就可以重用此功能

So I can reuse this function as long as I have the name of the listbox

下面的代码

Public Function getListBoxSelection(listBox As MSForms.listBox) As String
    Dim colCount As Integer
    Dim I As Long
    Dim selectedItem As String

    If listBox.ListIndex <> -1 Then
        For I = 0 To (listBox.ColumnCount - 1)
             selectedItem = selectedItem & listBox.column(I)
        Next I
    End If
    getListBoxSelection = selectedItem
End Function

提前谢谢!

推荐答案

您可以使用Worksheets("sheet1").OLEObjects("ListBox1").Object

You can use Worksheets("sheet1").OLEObjects("ListBox1").Object

这篇关于VBA如何通过名称访问列表框excel-2010的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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