excel学习库

excel表格_excel函数公式大全_execl从入门到精通

Excel 下拉列表的操作方法,这篇文章全讲清楚了

本节介绍一下ControlFormat对象,严格来说这个对象是对应于Excel中的ListBox对象的一些属性和方法,为什么不以ListBox来返回,就不太清楚了。

总之,要对Excel表中的ListBox对象操作就这么做就行了。

那么,如何得到ControlFormat对象呢?

用下面的方法:

dim xCF as Object

Set xCF=Shapes(i).ControlsFormat

对象xCF就是一个ControlFormat对象,其中i代表了此Shape的Index值。

有点乱,但要了解一下Shape对象,如下图所示,有按钮对象,下拉列表和文本框对象,都 属于Shapes对象合集。

可以使用Shape对象的ContrlFormat来返回ContrlFormat对象。

当我们得到这个ContrlFormat对象之后,就可以对下拉列表框进行各种添加删除操作了。

ContrlFormat对象有四个方法:Additem、List、RemoveAllitems、Removeitem

懂基本英语就基本明白这四种方法的功能了,这里不做过多介绍,具体可参考下面的代码进行对号。

更加重要的是ContrlFormat的属性,一共有17个,属性就是用一些固定的参数来进行设置,可以使下拉列表框更加符合要求,目的很简单。

下面,用实例来证明一下我们通过代码如何实现对下拉列表的添加、修改、删除等等操作。

添加列表框

Private Sub AddListBox()

DelListbox '删除除列表框

Dim xlobj As Object

'添加列表框

Set xlobj = Me.Shapes.AddFormControl(xlListBox, Range("E3").Left, Range("E3").Top, 200, 350)

Dim xFormat As Object

Set xFormat = xlobj.ControlFormat '返回列表对象

xFormat.RemoveAllItems '清除列表内容

xFormat.ListFillRange = Range("C4:C20").Address'设置列表区域

Set xFormat = Nothing

Set xlobj = Nothing

End Sub

返回列表值

Private Sub ShowListValue()

Dim xShape As Shape

For Each xShape In Me.Shapes

If xShape.Type = 8 Then

MsgBox xShape.ControlFormat.List(xShape.ControlFormat.ListIndex)

End If

Next xShape

End Sub

给列表框添加列表

Private Sub AddListItems()

Dim xShape As Shape

For Each xShape In Me.Shapes'遍历Shapes

If xShape.Type = 8 Then'如果是列表

xShape.ControlFormat.RemoveAllItems'清除所有列表值

For i = 4 To 7

xShape.ControlFormat.AddItem Range("B" & i).Value'添加列表

Next i

End If

Next xShape

End Sub

如上代码,根据ContrlFormat的四个方法和属性可实现所有对列表框的添加删除修改功能。

这样,就大大地增加了Excel工作表的一些使用功能。

欢迎关注、收藏

---END---

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2024年12月    »
1
2345678
9101112131415
16171819202122
23242526272829
3031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
    文章归档
      友情链接