Excel 中的列表框操作,可以很有序地實現選擇功能,本文將介紹一個對象,以實現對列表框的新建、刪除等操作 。

文章插圖
ControlFormat對象可以通過Shape對象的ControlFormat屬性返回 。
如下代碼:
Set xListObj = Shapes(2).ControlFormat
xListObj就是一個ControlFormat對象 。
ControlFormat方法和屬性
方法
屬性
AddItem
Application
List
Creator
RemoveAllItems
DropDownLines
RemoveItem
Enabled
LargeChange
LinkedCell
ListCount
ListFillRange
ListIndex
LockedText
Max
Min
MultiSelect
Parent
PrintObject
SmallChange
Value

文章插圖
如上圖所示,本示例實現多種方法給ListBox控件添加列表值 。
首先新建一個列表框,代碼如下:
PrivateSub AddListBox()'新建ListBox列表框DimxShape As ObjectSetxShape = Me.Shapes.AddFormControl(xlListBox, 100, 100, 210, 280)SetxShape = NothingEndSub 利用Additem 方法添加列表值
Private Sub AddListItems() '添加列表值 On Error Resume Next Dim xShape As Object Dim xListObj As Object SetxListObj = Shapes(2).ControlFormat WithxListObj .RemoveAllItems .AddItem "列表1".AddItem "列表2".AddItem "列表3"EndWithSetxListObj = NothingSetxShape = NothingEndSub

文章插圖
List方法添加列表值
Private Sub AddListItems() '添加列表值 On Error Resume Next Dim xShape As Object Dim xListObj As Object SetxListObj = Shapes(2).ControlFormat xListObj.List = Array("eee", "dddd", "fff") SetxListObj = NothingSetxShape = NothingEndSub ListFillRange屬性設置列表值
PrivateSub AddlistRange()'添加列表值OnError Resume NextDimxShape As ObjectDimxListObj As ObjectSetxListObj = Shapes(2).ControlFormatxListObj.ListFillRange= "B3:B10"SetxListObj = NothingSetxShape = NothingistFillRangeEndSub 刪除列表值
PrivateSub DelListItems()'刪除列表值OnError Resume NextDimxShape As ObjectDimxListObj As ObjectSetxListObj = Shapes(2).ControlFormatWithxListObj.RemoveItem.ListIndexEndWithSetxListObj = NothingSetxShape = NothingEndSub【附:所有代碼大全及語法 vba編程代碼大全】 ListBox列表框在編寫種類功能性應用時,非常方便,熟練掌握可大大提高對Excel表格的自動化應用技巧 。
推薦閱讀
- 新手vba基礎入門教程指南 vba編程代碼大全
- ipad所有型號和尺寸對照表 怎么看ipad是幾代
- 1分鐘解讀蘋果12手機所有參數配置 蘋果12手機分辨率是多少
- 所有藏寶圖的位置介紹 天國拯救鴿子籠下的寶藏在哪里
- 一文介紹lol所有英雄 英雄聯盟lol人物圖片大全和介紹
- 分享9個免費java開源代碼網站 java源代碼網站大全
- 火影忍者代碼大全可復制 火影忍者究極沖擊金手指代碼
- 蘋果手機微信恢復功能操作詳解 蘋果自帶微信恢復代碼
- html自動跳轉代碼大全 最新好看的html跳轉網頁源碼
- 超全超細微信恢復聊天記錄方法 蘋果自帶微信恢復代碼
