如何使用VBA将带按钮的行添加到excel表?

时间:2017-12-29 19:34:54

标签: excel-vba vba excel

目前我正在使用表单向excel中的表添加值:

Private Sub btnSave_Click()
    Dim lo As Excel.ListObject
    Dim newRow As Excel.ListRow
    Dim sheetName As String, tableName As String
    sheetName = "Dados"
    tableName = "articles"
    Set lo = getTable(sheetName, tableName)

    Dim ref As Integer
    ref = getMaxRef(tableName)

    Dim btnEdit As Button, btnDelete As Button
    'How can I add this 2 buttons to the newRow?

    Set newRow = lo.ListRows.Add(AlwaysInsert:=True)
    newRow.Range = Array(ref, cboStores.Value, cboTypes.Value, cboMaterials.Value, txtDescription.Value, txtWeight.Value, txtPrice.Value)

    Unload Me
End Sub

是否可以直接在newRow中添加按钮,或者在添加行后是否必须搜索单元格,然后添加按钮?

1 个答案:

答案 0 :(得分:0)

这是一般代码。它将按钮的尺寸与A1单元格的尺寸对齐:

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