2016-07-06 38 views
0

我有很多工作表和數百個複選框的工作簿,切換在Excel中選擇複選框等如何查找和使用VBA

某處在代碼中,我繼承了

「FlashCopy_chkbox.Enabled =假「

不起作用。我需要找到WHERE複選框/切換是在同一個文件的以前版本。

簡而言之,如何使用vba選擇引用的內容並將其集中在屏幕上?

+0

循環遍歷每個工作表中的所有對象並測試類型爲「checkbox」,然後測試名稱(或者只是測試名稱)。一旦你找到你需要的,激活它。 –

回答

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 

我明白你正在尋找一個內置的方法,但在未來,請張貼一些代碼。堆棧溢出在這裏幫助改進現有的代碼。

+0

謝謝,這有助於我進入工作表,然後.TopLeftCell.Address到準確的位置。 – bobo

+0

太棒了!很高興能夠幫助你。 – 2016-07-07 19:10:31

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 

讓我知道如果您有任何問題。

+0

謝謝 - .TopLeftCell.Address是我一直在尋找! – bobo