现在的位置: 首页 > 综合 > 正文

Excel ActiveX 简介—常用控件—–ComboBox

2011年03月10日 ⁄ 综合 ⁄ 共 1233字 ⁄ 字号 评论关闭

由于工作一直都比较忙,没有来的及 及时发博客,今天来看看,发现,这个系列还没弄完。接着来~

 

常用空间有Button, checkbox, combo box, list box, textbox, option button, 如果了解C# 控件的使用方法的话,那么这个是一样的用。以下代码主要是从某个sheet中获取某一列不重复的数据,填充到combo box

Sub InitComboBox (table, list As ComboBox)   '' Init ComboBox

 list.clear

 Dim sheet     As Worksheet

 Set sheet = Sheets(table)

 Dim used     As Range

 Set used = sheet.UsedRange

 Dim rowcount     As Long

 Dim rowcontent As Variant

 rowcount = used.Rows.count

 rowcontent = sheet.cells(startUsedRow, startUsedCol)

 list.AddItem rowcontent

 For i = startUsedRow + 1 To rowcount Step 1

      rowcontent = sheet.cells(i, startUsedCol)

 If IsInComBoxContent(rowcontent, list) = False Then list.AddItem rowcontent

 Next i

 

End Sub

'' Judge whether the value found in the excel is in Combobox

Private Function IsInComBoxContent(data, list As ComboBox) As Boolean

IsInComBoxContent = False

For i = 0 To list.ListCount - 1 Step 1

If data = list.list(i) Then

    IsInComBoxContent = True

    Exit For

    End If

   Next i

End Function

 

在这里用到了:

List.list(i) :获取某个指定的第i个数据

List.add 方法 添加到list 里面去

如果在使用的过程中,遇到问题,可以去往上多查点资料。

http://www.51vba.com/list.aspx?cid=39 

 

在这里顺便提一下:if…then…else 的使用中遇到的问题,一般then 后面紧跟的一个语句或者一个函数,或者一个过程,而不能是多个串行执行的语句。例如上面的语句中,

If data = list.list(i) Then

    IsInComBoxContent = True

    Exit For

    End If

 

如果在    IsInComBoxContent = True 还有其他语句,而不是exit,那么这时候,程序的执行是有问题的,可以把这多个语句连接起来放到一个过程或者函数里面去。

抱歉!评论已关闭.