在excel中找控件的问题

iseelxj 2005-10-31 02:54:58
excel中有个sheet,sheet上面有个下拉框。怎么用代码找到这个下拉框那?
是不是像下面那样?可是具体的怎么作那?谢谢!!
for each *** in ***
if ***.*** = *** then

end if
next
...全文
187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2005-11-05
  • 打赏
  • 举报
回复
还是循环找oleobject要好一些:

Private Sub CommandButton1_Click()
ActiveSheet.OLEObjects.Add ClassType:="Forms.combobox.1", Link:=False, DisplayAsIcon:=False, Left:=ActiveCell.Left, Top:=ActiveCell.Top, Width:=ActiveCell.Width, Height:=ActiveCell.Height
End Sub



Private Sub mtd(ByRef obj As OLEObject)
ActiveSheet.OLEObjects(obj.Name).Object.List = Array(1, 2, 3, 4, 5)
MsgBox ActiveSheet.OLEObjects(obj.Name).Object.ListCount
End Sub

Private Sub CommandButton2_Click()
Dim ctl As OLEObject
For Each ctl In Worksheets("Sheet1").OLEObjects
If ctl.Name = "ComboBox1" Then mtd ctl
Next
End Sub
iseelxj 2005-11-01
  • 打赏
  • 举报
回复
狼行天下的方法可行,但是如果combobox是动态生成的就会出编译错误。我现在想做的是,打开workbook,根据当前日期生成新sheet,再从模板sheet中拷贝combobox到新sheet。考过来后,知道combobox的名字,根据名字找到这个combobox,在传到一个参数为combobox的函数中。可是现在的问题是,我可以循环找oleobject,也可以循环找shape,但是找到后,传不进去。“类型错误”!不知道怎么解决!
northwolves 2005-10-31
  • 打赏
  • 举报
回复
Private Sub CommandButton1_Click()
Dim ctl As Shape
For Each ctl In Worksheets("Sheet1").Shapes
If ctl.Name = "ComboBox1" Then ctl.Left = 0
Next
End Sub

Private Sub mtd(ByRef obj As ComboBox)
obj.Left = 500
End Sub

Private Sub CommandButton2_Click()
mtd ComboBox1
End Sub


也可以这样调用:

Private Sub CommandButton3_Click()
Worksheets("Sheet1").Shapes("combobox1").Top = 100
End Sub
fishmans 2005-10-31
  • 打赏
  • 举报
回复
private sub mtd(obj as Object)

end sub
试试?
iseelxj 2005-10-31
  • 打赏
  • 举报
回复
不好意思错了
private sub mtd(byref obj as TextBox)

end sub
iseelxj 2005-10-31
  • 打赏
  • 举报
回复
这样作是能找到,但如果还有个函数的参数是textbox,要是把p当参数穿过去的话就会出错,怎么办?
for each p in Doc.Sheets("sheet1").OLEObjects
if p.name="text1" then
mtd(p)
end if
next

private sub mtd(byval obj as TextBox)

end sub
crycoming 2005-10-31
  • 打赏
  • 举报
回复
for each p in Doc.Sheets("sheet1").OLEObjects
if p.name="text1" then
end if
next

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧