Microsoft ActiveX控件

时间:2016-09-08 14:49:27

标签: excel vba excel-vba excel-2010

有没有办法更改插入电子表格的ActiveX按钮的索引值。我目前有四个按钮,两个隐藏,两个可见。我想重新订购它们,以便在对象之间没有很大的差距。我有一些VBA代码在文档打开时运行,以确保它们的大小和位置正确。因为它遍历OLEObjects集合;它们在电子表格中的顺序无关紧要,因为OLE对象集合中的索引值,它们总是会出现间隙。以下是代码:

Private Sub Workbook_Open()
Application.ErrorCheckingOptions.EvaluateToError = False
ActiveWorkbook.Worksheets("SITE").Activate

Dim button As OLEObject
Dim name As String, top As Integer

top = 15

For Each button In ActiveWorkbook.Worksheets("SITE").OLEObjects
    Debug.Print button.name & " " & button.ZOrder
    name = button.name
    If button.OLEType = xlButtonOnly And InStr(name, "btn") = 1 Then
        With button
            .Height = 21.75
            .Width = 174.75
            .Left = 1114.5
            .top = top
        End With
        top = top + 30
    End If
Next button

End Sub

1 个答案:

答案 0 :(得分:0)

如果你给它们正确的名称,其中包含一个反映其预定位置的整数代码(例如:“btn ... 01”,“btn ... 02”,...)那么你可以尝试这个代码(对不起)因为现在无法将其格式化为代码):

Private Sub Workbook_Open()
Application.ErrorCheckingOptions.EvaluateToError = False
ActiveWorkbook.Worksheets("SITE").Activate

Dim button As OLEObject
Dim name As String
Dim btnRnk As Long

For Each button In ActiveWorkbook.Worksheets("SITE").OLEObjects
    name = button.name
    If button.OLEType = xlButtonOnly And InStr(name, "btn") = 1 Then
        btnRnk = CLng(Right(name,2))
        With button
            .Height = 21.75
            .Width = 174.75
            .Left = 1114.5
            .top = 15 + (btnRank - 1) * 30
        End With
    End If
Next button

End Sub