How do I inspect the name of each combobox on a worksheet fo a particular name using VBA-Excell?

本秂侑毒 提交于 2019-12-13 02:42:43

问题


I have several comboboxes on a worksheet whose object names contain the word Product. I would like to select each of these comboboxes in order to update the list items.

I have managed to identify comboboxes but I can't seem to identify the name specifically.

dim CBO as oleboject
set ws = sheets(1)
with sheets(1)
for each cbo in ws.oleobjects
if typename(cbo.object) = "ComboBox" then
 THE CHECK HERE FAILS
end if
next cbo
end with

I can't get the code to identify the name of the object.


回答1:


So I think you need to do a prior check to ensure that the typename property exists:

Sub ComboLoop()
Dim Ws As Worksheet
Dim OleObj As OLEObject

For Each Ws In ThisWorkbook.Worksheets
   For Each OleObj In Ws.OLEObjects
      If OleObj.OLEType = xlOLEControl Then
         If TypeName(OleObj.Object) = "ComboBox" Then
             With OleObj.Object

             'SOME ACTIONS 
             End With
         End If
      End If
   Next OleObj
Next Ws
End Sub

I've used this code in the past and it worked for me so hope it helps.




回答2:


You are almost there. Instead of using

CBO.List = array(item1,item2),

you need to use

CBO.Object.List = array(item1,item2)



来源:https://stackoverflow.com/questions/56721002/how-do-i-inspect-the-name-of-each-combobox-on-a-worksheet-fo-a-particular-name-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!