我需要在工作表上列出大量舊的ActiveX命令按鈕的標題。如何獲得CommandButton集合?
我該如何參考收藏?
僞代碼:
For each btn in Activesheet.CommandButtons
Debug.Print btn.Caption
Next btn
我需要在工作表上列出大量舊的ActiveX命令按鈕的標題。如何獲得CommandButton集合?
我該如何參考收藏?
僞代碼:
For each btn in Activesheet.CommandButtons
Debug.Print btn.Caption
Next btn
Dim s As Worksheet
Set s = ActiveSheet
Dim o As OLEObject
For Each o In s.OLEObjects
If TypeName(o.Object) = "CommandButton" Then
Debug.Print o.Object.Caption
End If
Next
如何通過OLEObjects
在工作表循環,您可以使用自己progID
,看到它,它等於「Forms.CommandButton.1」。
注意:請不要使用ActiveSheet
,而應使用完全限定的對象,如Worksheets("Sheet1")
。
Option Explicit
Sub FindCommandButtonsInOLEObjects()
Dim Sht As Worksheet
Dim Obj As OLEObject
Set Sht = ThisWorkbook.Sheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
' loop thourgh all OLE objects in "Sheet1"
For Each Obj In Sht.OLEObjects
If Obj.progID Like "Forms.CommandButton.1" Then ' <-- check if current object is type Comman Button
Debug.Print Obj.Object.Caption
End If
Next Obj
End Sub
更新了我從MrExcel網站上找到的代碼:
Dim BtnActX As Integer
Dim MyShapes As OLEObjects
Dim Btn As OLEObject
'OLE Programmatic Identifiers for Commandbuttons = Forms.CommandButton.1
Set MyShapes = ActiveSheet.OLEObjects
For Each Btn In MyShapes
If Btn.progID = "Forms.CommandButton.1" Then
BtnActX = BtnActX + 1
abc = Btn.Object.Caption
MsgBox "command button text is: " & abc
End If
Next