将表单按钮插入单元格但格式按钮比单元格尺寸更小

时间:2018-04-13 13:21:16

标签: vba excel-vba excel

如果列" A"中有值,我创建了以下代码来插入表单按钮。我试图让窗体按钮略小于单元尺寸(不要碰到单元格壁)。这就是我到目前为止所做的:

Sub InsertButtons()
Dim i As Long
Dim shp As Object
Dim dblLeft As Double
Dim dblTop As Double
Dim dblWidth As Double
Dim dblHeight As Double


With Sheets("MailMerge")

    dblLeft = .Columns("N:N").Left 
    dblWidth = .Columns("N:N").Width - 1

    For i = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
        dblHeight = .Rows(i).Height -1
        dblTop = .Rows(i).Top

        If .Cells(i, 1).Value = Empty Then

        Else

            Set shp = .Buttons.Add(dblLeft, dblTop, dblWidth, dblHeight)
            shp.OnAction = "SendEmail"
            shp.Characters.Text = "Email"

        End If

    Next i

End With

Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = False

End Sub

1 个答案:

答案 0 :(得分:0)

您需要从宽度和高度中减去2。并在顶部和左侧添加1

dblLeft = .Columns("N:N").Left + 1
dblWidth = .Columns("N:N").Width - 2

dblHeight = .Rows(i).Height - 2
blTop = .Rows(i).Top + 1

我还建议使用

If Not .Cells(i, 1).Value = Empty Then
    Set shp = .Buttons.Add(dblLeft, dblTop, dblWidth, dblHeight)
    shp.OnAction = "SendEmail"
    shp.Characters.Text = "Email"
End If

而不是空If语句。