VBA控件ListBox的BoundColumn和TextColumn用法,Value和Text的用法

       在使用Excel编写VBA程序时,用到ListBox,然后研究了下它的所有属性。其实这个控件功能很不好用,太老了,最重要的是还不支持鼠标滚轮,很不好操作,但是考虑到兼容性,还是使用它。

 

       其实读取、写入数据用ListBox.List已经足够了,而BoundColumn和TextColumn主要是用于读取数据,有什么鸟用?我也不是很清楚,但是可以像下面这样用。这两个参数相当于设置了列的索引(j),只需选中行(i)即可读取对应的数据List(i,j)。其中,BoundColumn与Value关联;TextColumn与Text关联。

Private Sub CommandButton1_Click()
     
    ListBox1.ListIndex = 0      '选中第一行,不选中的话,读取不到数值,
    ListBox1.BoundColumn = 2    '该属性从1开始计数,即现在对应的列索引为1;默认值为1
    ListBox1.TextColumn = 3     '该属性从1开始计数,即现在对应的列索引为2;默认值为-1,如果不赋值将无法读取数据
    Debug.Print "value = " & ListBox1.Value '输出 value = 0,1
    Debug.Print "text = " & ListBox1.Text   '输出 text = 0,2

End Sub

Private Sub UserForm_Initialize()
    Dim i As Integer
    Dim j As Integer
    
    ListBox1.ColumnCount = 5    '设置5列
    i = 0
    While i < 10
        ListBox1.AddItem
        j = 0
        While j < ListBox1.ColumnCount
            ListBox1.List(i, j) = CStr(i) & "," & CStr(j)
            j = j + 1
        Wend
        i = i + 1
    Wend
End Sub

 

关键词:VBA  ListBox BoundColumn TextColumn Value Text 说明 参数 使用方法 Excel

posted on 2016-01-10 11:12  雷锋123  阅读(11297)  评论(0编辑  收藏  举报

导航