问题
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