【问题标题】:VBA excel macro - add rows above buttonVBA excel宏 - 在按钮上方添加行
【发布时间】:2020-07-27 17:33:13
【问题描述】:

在我的工作表中,我正在尝试创建 VBA 代码,这将允许我: 1)在单击按钮上方添加特定(恒定)行数 2)以适当的方式格式化它们

我所知道的是每个对象(形状)都存储 TopLeftCell 的值,我可以从中获取行号。 我可以简单地在工作表中的特定行上方或下方添加行

ActiveSheet.Rows(21).Insert shift:=xlShiftDown, CopyOrigin:=xlFormatFromLeftOrAbove

我想为 ActiveSheet 中的任何按钮创建函数 addNewRows2 的位置 - 它获取按钮的位置(行号)并在其上方添加行

Private Sub CommandButton2_Click()
addNewRows2
End Sub

Function addNewRows2()
   Application.ActiveSheet.Buttons(Application.Caller).TopLeftCell.EntireRow.Insert 
End Function

【问题讨论】:

  • 您的问题/问题是什么?

标签: excel vba


【解决方案1】:

在 Class 模块 cButton 中放入以下行

Public WithEvents MyButton As MSForms.CommandButton

Private Sub MyButton_Click()
    ActiveSheet.Rows(MyButton.TopLeftCell.Row).Insert
End Sub

然后在标准模块中放代码

Dim TheCommandButtons() As New cButton

Sub Activate_ActiveX_CommandButtons()
    Dim shp As Shape, iButtonCount As Long
    ReDim TheCommandButtons(1 To 1)
    iButtonCount = 0
    For Each shp In ActiveSheet.Shapes
        If shp.OLEFormat.Object.OLEType = xlOLEControl Then
            iButtonCount = iButtonCount + 1
            ReDim Preserve TheCommandButtons(1 To iButtonCount)
            Set TheCommandButtons(iButtonCount).MyButton = shp.OLEFormat.Object.Object
        End If
    Next shp
End Sub

现在运行代码Activate_ActiveX_CommandButtons,之后你可以使用任何命令按钮来完成在按钮上方插入一行的任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多