首页 > 解决方案 > 如何使用 VBA-Excell 检查特定名称的工作表上每个组合框的名称?

问题描述

我在工作表上有几个组合框,其对象名称包含单词 Product。我想选择每个组合框以更新列表项。

我已经设法识别组合框,但我似乎无法具体识别名称。

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

我无法获得识别对象名称的代码。

标签: excelvbalistbox

解决方案


所以我认为您需要事先检查以确保 typename 属性存在:

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

我过去使用过这段代码,它对我有用,所以希望它有所帮助。


推荐阅读