Home > OS >  Add column value in table with button click Excel
Add column value in table with button click Excel

Time:06-03

I want to create a column value number will increase with button click.

Let's say the table is Cust_Table, and it has columns Cust Name and Total Contacting.

I want to add all value with " 1" every time I click the button.

Example:

  • Cust A: total contact 1
  • Cust B: total contact 2
  • Cust C: total contact 5
  • Cust D: total contact 3

After I click the button, those values will be increased by 1:

  • Cust A: total contact 2
  • Cust B: total contact 3
  • Cust C: total contact 6
  • Cust D: total contact 4

Thanks to @Tim Williams, the code works...

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

See this screenshot

CodePudding user response:

Once you have a reference to a listobject, you can access the range for a given column heading like this:

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
  • Related