按钮位置错误

时间:2019-12-02 21:34:49

标签: excel vba

我在单元格内放置按钮时遇到问题。该按钮的位置不正确。

Public Sub TableDisplayButton()
    Dim cr As Range, cr2 As Range
    With ActiveSheet.Shapes(Application.Caller)
    Set cr = .TopLeftCell
    Set cr2 = .BottomRightCell
    MsgBox cr.Address & " " & cr.Left
    .Left = cr.Left
    .Top = cr.Top
    .Width = Range(cr, cr2).Width
    .Height = Range(cr, cr2).Height
    end with
end sub

该按钮向左稍微移至相邻的左单元格,再次单击该按钮将重新定位到另一个单元格。单击按钮将运行此子例程TableDisplayButton

cr.address显示正确的单元格。

有人知道是什么原因吗? .left.top是否有可能将小数点后一位舍入为整数值?

1 个答案:

答案 0 :(得分:0)

Sub AlignByCell()
    Dim btn As Button
    With Range("A1")
        Set btn = Sheet1.Buttons.Add(Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    End With
End Sub