如何使用VBA在Excel中查找和选择Checkbox

时间:2016-07-06 19:36:42

标签: excel-vba vba excel

我有一个包含许多工作表和数百个复选框,切换等的工作簿。

代码中的某处我继承了

“FlashCopy_chkbox.Enabled = False”

不起作用。我需要找到WHERE那个复选框/切换位于同一文件的先前版本中。

简单地说,如何使用vba选择引用的内容并将其置于屏幕中心?

2 个答案:

答案 0 :(得分:2)

以下代码将列出VBE Immediate窗口中工作簿中所有工作表上的所有ActiveX控件。此外,代码会在所有表格中列出所有表单控件OptionButtonsCheckBoxesButtons

Option Explicit

Public Sub FindThemAll()

Dim ws As Worksheet
Dim obj As OLEObject
Dim opt As OptionButton
Dim chk As CheckBox
Dim cmd As Button

For Each ws In ThisWorkbook.Worksheets
    'Handling all ActiveX controls
    For Each obj In ws.OLEObjects
        Debug.Print "---------------------------------------------"
        Debug.Print "ActiveX component on sheet: " & ws.Name
        Debug.Print "Location on sheet: " & obj.TopLeftCell.Address
        Debug.Print "Name of the component: " & obj.Name
        Debug.Print "Object type: " & TypeName(obj.Object)
    Next obj
    'Handling Form Controls
    For Each opt In ws.OptionButtons
        Debug.Print "---------------------------------------------"
        Debug.Print "Form control on sheet: " & ws.Name
        Debug.Print "Location on sheet: " & opt.TopLeftCell.Address
        Debug.Print "Name of the component: " & opt.Name
        Debug.Print "Object type: OptionButton"
    Next opt
    For Each chk In ws.CheckBoxes
        Debug.Print "---------------------------------------------"
        Debug.Print "Form control on sheet: " & ws.Name
        Debug.Print "Location on sheet: " & chk.TopLeftCell.Address
        Debug.Print "Name of the component: " & chk.Name
        Debug.Print "Object type: CheckBox"
    Next chk
    For Each cmd In ws.Buttons
        Debug.Print "---------------------------------------------"
        Debug.Print "Form control on sheet: " & ws.Name
        Debug.Print "Location on sheet: " & cmd.TopLeftCell.Address
        Debug.Print "Name of the component: " & cmd.Name
        Debug.Print "Object type: Button"
    Next cmd
Next ws

End Sub

如果您有任何问题,请与我们联系。

答案 1 :(得分:0)

您不能直接从工作表模块外部调用工作表上的控件。 FlashCopy_chkbox在脚本中的某处声明了吗? Set FlashCopy_chkbox = Worksheet("Sheet1").FlashCopy_chkbox

无论如何,这都会找到它。

Sub LookForFlash()
    Dim ws As Worksheet
    Dim ck As OLEObject

    For Each ws In ThisWorkbook.Worksheets
        On Error Resume Next
        Set ck = ws.OLEObjects("FlashCopy_chkbox")
        If Err = 0 Then
            MsgBox ws.Name
        End If
        On Error GoTo 0
    Next ws
End Sub

我知道您正在寻找内置方法,但将来请发布一些代码。 Stack Overflow旨在帮助改进现有代码。