【问题标题】:Excel VBA Button AnchorExcel VBA 按钮锚点
【发布时间】:2021-09-13 12:35:44
【问题描述】:

我有多个按钮,它们位于不同表格的右侧,全部垂直堆叠。当我按下按钮时,它会在表格顶部添加一个新行并将其他行向下移动 - 这正是我想要的。然而,这些按钮似乎并没有随着表格的第一行移动,所以在每次点击几下之后,一切都没有对齐。我怎样才能让我的按钮固定在各自表格的第一行。

这是我的代码:

Private Sub CommandButton1_Click()
Dim mySheets
    Dim i As Long
    
    mySheets = Array("Highland")
    
    For i = LBound(mySheets) To UBound(mySheets)
        With Sheets(mySheets(i))
            .Range("OP_DATE").EntireRow.Insert Shift:=xlDown
            .Range("OP_DATE:lineOP").Borders.Weight = xlThin
        End With
    Next i
End Sub

Private Sub CommandButton2_Click()
Dim mySheets
    Dim i As Long
    
    mySheets = Array("Highland")
    
    For i = LBound(mySheets) To UBound(mySheets)
        With Sheets(mySheets(i))
            .Range("P_DATE").EntireRow.Insert Shift:=xlDown
            .Range("P_DATE:lineP").Borders.Weight = xlThin
        End With
    Next i
End Sub

Private Sub CommandButton3_Click()
Dim mySheets
    Dim i As Long
    
    mySheets = Array("Highland")
    
    For i = LBound(mySheets) To UBound(mySheets)
        With Sheets(mySheets(i))
            .Range("S_DATE").EntireRow.Insert Shift:=xlDown
            .Range("S_DATE:lineS").Borders.Weight = xlThin
        End With
    Next i
End Sub

【问题讨论】:

    标签: excel vba button


    【解决方案1】:

    您可能需要尝试一下,但应该非常接近。如果要添加多行,则需要将函数的返回乘以该数字。

    此函数以缇为单位获取行高和列宽。

    Public Function GetRowColumnTwips(r As Long, c As Long) As Variant
        Dim twips(1) As Long
        
        twips(0) = Rows(r).Height
        twips(1) = Columns(c).Width 'I put this in if you need it but I'm not actually using it here
        
        GetRowColumnTwips = twips
    End Function
    

    这个 sub 使用行高值来保持它在工作表上的位置。

    Sub Button2_Click()
        Dim twips As Variant
        With Sheet1
            twips = GetRowColumnTwips(.Shapes(Application.Caller).TopLeftCell.Row, .Shapes(Application.Caller).TopLeftCell.Column)
            .Shapes(Application.Caller).Top = .Shapes(Application.Caller).Top - twips(0)
        End With
    End Sub
    

    您可以将按钮事件子代码放入任何按钮代码中,唯一需要更改的是With 语句中的工作表。

    在您的第一个按钮的代码中:

    Private Sub CommandButton1_Click()
    Dim mySheets
        Dim i As Long
        Dim twips As Variant
        mySheets = Array("Highland")
        
        For i = LBound(mySheets) To UBound(mySheets)
            With Sheets(mySheets(i))
                .Range("OP_DATE").EntireRow.Insert Shift:=xlDown
                .Range("OP_DATE:lineOP").Borders.Weight = xlThin
                twips = Module1.GetRowColumnTwips(.Shapes(Application.Caller).TopLeftCell.Row, .Shapes(Application.Caller).TopLeftCell.Column)
                .Shapes(Application.Caller).Top = .Shapes(Application.Caller).Top - twips(0)
            End With
        Next i
    End Sub
    

    对于 ActiveX 控件,您需要按名称引用每个按钮,而不是依赖调用形状。

    twips = Module1.GetRowColumnTwips(.OLEObjects("CommandButton1").TopLeftCell.Row, .OLEObjects("CommandButton1").TopLeftCell.Column)
    .OLEObjects("CommandButton1").Top = .OLEObjects("CommandButton1").Top - twips(0)
    

    【讨论】:

    • 感谢您到目前为止的帮助 - 我对此很陌生,我应该将上述功能 sub 放到哪里?
    • 您可以放置​​在模块中的函数或工作表的代码隐藏并不重要,也就是说,您可以在插入行后放置代码的子代码。由于您已经有了 With 语句,您真的只需要 DimWith 中的两行
    • 谢谢!我将该函数放入一个单独的模块中,但现在我收到一个运行时错误,提示找不到具有指定名称的项目,并且当我调试它时,它会将我带到按钮子的 Twips = 行。任何额外的帮助将不胜感激
    • 哦,你可能需要做Module1.GetRowColumnTwips 或任何模块名称,然后在函数定义前面放一个Public
    • 我想我越来越近了!我的函数在模块 2 中,所以我将其设为公共函数并修改了 twips 语句: twips = Module2.GetRowColumnTwips(.Shapes(Application.Caller).TopLeftCell.Row, .Shapes(Application.Caller).TopLeftCell.Column) 但是我仍然遇到同样的错误
    猜你喜欢
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    相关资源
    最近更新 更多