单击按钮 Excel 在 table 中添加列值

Add column value in table with button click Excel

我想创建一个列,数值会随着按钮的点击而增加。

假设 table 是 Cust_Table,它有列 Cust NameTotal Contacting

我想在每次点击按钮时将所有值都加上“+1”。

示例:

点击按钮后,这些值将增加1:

感谢 @Tim Williams,代码有效...

Private Sub CommandButton1_Click()

Dim lo As ListObject, c As Range, v As Long

v = ActiveSheet.Range("D2").Value 'cell with value to be added
Set lo = ActiveSheet.ListObjects("Cust_Table")
For Each c In lo.ListColumns("Total Contacting").DataBodyRange.Cells
    c.Value = c.Value + v
Next c

End Sub

获得对列表对象的引用后,您可以像这样访问给定列标题的范围:

Dim lo as listobject, c as range, v as long

v = ActiveSheet.Range("D2").value 'cell with value to be added
Set lo = ActiveSheet.ListObjects("Cust_Table")
for each c in lo.listcolumns("Total Contacting").DataBodyRange.Cells
    c.value = c.value + v
next c