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

问题描述 投票:0回答:1

目前我正在使用表单向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中添加按钮,或者在添加行后是否必须搜索单元格,然后添加按钮?

excel-vba vba excel
1个回答
0
投票

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

Sub Test()
    Dim btn As Button
    With Range("A1")
        Set btn = Sheet1.Buttons.Add(.Left, .Top, .Width, .Height)
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.