【问题标题】:Referring to cell relative to a ActiveX commandbutton VBA引用相对于 ActiveX 命令按钮 VBA 的单元格
【发布时间】:2012-07-17 09:32:55
【问题描述】:

我在 Excel 的许多不同工作表上都有许多 ActiveX 命令按钮。

当单击任何按钮时,我想在按钮旁边的单元格中放置一个字符串。

到目前为止,这一直很麻烦,因为我不能像使用 Form control 按钮那样询问按钮的 TopLeftCell,而只能询问控件的 TopLeft 值。将这些转换为行和列并非易事,因为每张纸上都有很多不同的单元格高度/宽度。

我想防止在每个button_Click() 中硬编码一个单元格位置,这样我就可以毫无问题地移动按钮,修改工作表,......所以我不必突然更改每个按钮的代码文本必须在按钮旁边出现 2 个单元格而不是 1 个单元格。

【问题讨论】:

    标签: excel button vba


    【解决方案1】:

    您可以使用here by Andrew Poulson 概述的此类模块方法来满足所有按钮的需求

    我不明白您对TopLeftCell 的评论 - 它可用于 ActiveX 按钮

    Class Module

    `named as Class1
    
    Public WithEvents ButtonGroup As CommandButton
    
    Private Sub ButtonGroup_Click()
        Sheets(ButtonGroup.Parent.Name).Cells(ButtonGroup.TopLeftCell.Row, ButtonGroup.TopLeftCell.Column) = "Done"
    End Sub
    

    Normal Module

    Dim Buttons() As New Class1
    
    Sub Class_Init()
        Dim Sh As Worksheet
        Dim Obj As OLEObject
        Dim ButtonCount As Integer
        For Each Sh In ThisWorkbook.Worksheets
            For Each Obj In Sh.OLEObjects
                If TypeName(Obj.Object) = "CommandButton" Then
                    ButtonCount = ButtonCount + 1
                    ReDim Preserve Buttons(1 To ButtonCount)
                    Set Buttons(ButtonCount).ButtonGroup = Obj.Object
                End If
            Next Obj
        Next Sh
    End Sub
    

    【讨论】:

    • 谢谢!你说得对,topleftcell 也可用于 ActiveX 按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    相关资源
    最近更新 更多