VBA 在表格中创建和使用下拉框(xlDropdow)
2013-06-05 14:59
651 查看
http://forums.codeguru.com/showthread.php?495404-how-to-add-items-to-the-comboBox-using-VBA
ghost56
Junior Member
Join DateApr 2010Posts7
Set MyCombo = Shapes.AddFormControl(xlDropDown, Cells(10, 1).Left, Cells(10, 1).Top, 50, Cells(10, 1).RowHeight)
My issues are:
1.Now how to add items to the comboBox1.
2. How to create a comboBox 2 adjacent to comboBox1 when an item is clicked in comboBox1.
when i used .AddItem methosd it throws an error stating that method or property does not exist for the object. Can anyone help me to resolve these issues.
Cimperiali
Old Uncle ModeratorPower Poster
Join DateJul 2000LocationMilano, ItalyPosts7,724
In case, a way to do it (but user must enable macros) is the following
in a worksheet:
Code:
ghost56
Junior Member
Join DateApr 2010Posts7
how to add items to the comboBox using VBA
I have created a comboBox1 on an excel sheet with the following statementSet MyCombo = Shapes.AddFormControl(xlDropDown, Cells(10, 1).Left, Cells(10, 1).Top, 50, Cells(10, 1).RowHeight)
My issues are:
1.Now how to add items to the comboBox1.
2. How to create a comboBox 2 adjacent to comboBox1 when an item is clicked in comboBox1.
when i used .AddItem methosd it throws an error stating that method or property does not exist for the object. Can anyone help me to resolve these issues.
Cimperiali
Old Uncle ModeratorPower Poster
Join DateJul 2000LocationMilano, ItalyPosts7,724
Re: how to add items to the comboBox using VBA
Not sure this is a dot net question. Seems as if you're doing it inside excel.In case, a way to do it (but user must enable macros) is the following
in a worksheet:
Code:
Private Sub Worksheet_Activate() Dim shp As Shape For Each shp In Me.Shapes If shp.Name = "cboDynamic" Then shp.Delete Set shp = Nothing Exit For End If Set shp = Nothing Next If shp Is Nothing Then CreateCbo End If End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) End Sub Private Sub CreateCbo() With Me.Shapes.AddFormControl(xlDropDown, Left:=Cells(10, 1).Left, Top:=Cells(10, 1).Top, Width:=50, Height:=Cells(10, 1).RowHeight) .ControlFormat.DropDownLines = 3 .ControlFormat.AddItem "First", 1 .ControlFormat.AddItem "Second", 2 .ControlFormat.AddItem "Third", 3 .Name = "cboDynamic" .OnAction = "cboDynamic_Change" End With End SubPublic Sub cboDynamic_Change() Dim selectIndex As Integer 'delete any existing dropdown box named "cboDynSecond" 'if none exists, do not throw error On Error Resume Next ActiveSheet.DropDowns("cboDynSecond").Delete 'disable on error resume next On Error GoTo 0 selectIndex = ActiveSheet.DropDowns("cboDynamic").ListIndex Dim dd As DropDown Set dd = ActiveSheet.DropDowns("cboDynamic") Dim lft As Integer lft = dd.Left + dd.Width + 10 Dim tp As Integer tp = dd.Top Dim wdt As Integer wdt = dd.Width Dim hgt As Integer hgt = dd.Height Set dd = Nothing With ActiveSheet.Shapes.AddFormControl(xlDropDown, Left:=lft, Top:=tp, Width:=wdt, Height:=hgt) .ControlFormat.DropDownLines = 7 .Name = "cboDynSecond" Select Case selectIndex Case 1 .ControlFormat.AddItem "A_01", 1 .ControlFormat.AddItem "A_02", 2 .ControlFormat.AddItem "A_03", 3 .ControlFormat.AddItem "A_04", 4 Case 2 .ControlFormat.AddItem "B_01", 1 .ControlFormat.AddItem "B_02", 2 .ControlFormat.AddItem "B_03", 3 .ControlFormat.AddItem "B_04", 4 Case 3 .ControlFormat.AddItem "C1", 1 .ControlFormat.AddItem "C2", 2 .ControlFormat.AddItem "C3", 3 .ControlFormat.AddItem "C4", 4 End Select .OnAction = "cboDynSecond_Click" End With End Sub Public Sub cboDynSecond_Click() Dim shp As Shape For Each shp In ActiveSheet.Shapes If shp.Name = "cboDynSecond" Then Exit For End If Set shp = Nothing Next If Not shp Is Nothing Then MsgBox ("you clicked on itme index" & shp.ControlFormat.ListIndex _ & " (value is index: " & shp.ControlFormat.Value _ & " -while text is " & shp.ControlFormat.List(shp.ControlFormat.ListIndex) _ & ")") End If End Sub
相关文章推荐
- 使用 UITableView 创建表格应用演练(2)——从plist文件加载并显示数据
- 使用JavaScript和DOM动态创建表格
- crm使用soap创建下拉框
- 使用 UITableView 创建表格应用演练(3)——使用区段分类显示表格数据
- 使用HtmlControl动态创建一个表格
- 使用JS创建表格以及隔行换色(包括隔N行换色)
- HTML之表格的创建以及使用
- 使用 UITableView 创建表格应用演练(4)——自定义单元格
- 使用jquery-datatable和bootsrap创建表格
- 使用JavaScript和DOM动态创建表格
- 使用itext创建PDF文档-导出大表格-每页显示标题-项目实战
- 使用JavaScript和DOM动态创建表格
- 创建动态链接库,使用自己的dll中的排序算法,用Qt将排序用时在界面上用表格的形式呈现出来
- 使用报表工具FastReport.NET在XAML中创建表格
- 创建响应式布局--显示表格数据,使用测量单位,使用媒体查询
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- 如何使用THML创建表格
- Markdown使用table标签创建表格的问题
- 【javascript】使用javascript动态创建表格行
- 使用php创建mysql的表格