VBA with a button to add a row to a table and insert data

goamnor

New Member
Joined
Jul 23, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I hope you are doing well. I am completly new to the VBA topic.

I was aked to do a VBA, that start when clicking a button. It should add a new row under the existing one in my case the start is row 24 and it should continue therafter. The popup should show the date of today and I should be able to add names to one column. The next column should show hand tipped notes about calls. All the data should be put in in the VBA and added to the last row below.

Has anyone an idea how to satrt or maybe has something similar with a code i can use.

Best
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi goamnor, welcome to the MrExcel Forum.

This would work better if you had a form to open and fill out the information that you want pasted to the table. That said this code uses two input boxes to get your information.
The code assumes that your button is named "CommandButton1", your active sheet is "Sheet1" and your table name is "Table1". To make this work as is, this code must be placed in the "Sheet1" (or whatever sheet you are using) module in the Visual Basic Editor.

VBA Code:
Private Sub CommandButton1_Click()
    
    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim tbl As Object
    Dim nam As String, nte As String
    
    Set tbl = ws.ListObjects("Table1")
    ActiveSheet.ListObjects("Table1").ListRows.Add AlwaysInsert:=True
    nam = Application.InputBox("Input Name", , , , , , , 2)
    nte = Application.InputBox("Input Note for " & nam & " on " & Date, , , , , , , 2)
    tbl.DataBodyRange(tbl.ListRows.Count, 1) = Date
    tbl.DataBodyRange(tbl.ListRows.Count, 2) = nam
    tbl.DataBodyRange(tbl.ListRows.Count, 3) = nte
    
End Sub

Book1
ABCDE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23DATENAMENOTES
247/23/2024JOHN SMITHPLACED 3 ORDERS
257/24/2024JANE DOE30 DAYS PAST DUE
267/24/2024BOB BARKERWRONG PIRCE GIVEN
27
28
29
30
Sheet1
 
Upvote 0
Hello,

thank you for your support.

Is there any possibility to use something like a drop down menu for the names. The contact information will be above in roow 16-22. Would be great if I can just click on a name and it gets inserted.

Best
 
Upvote 0
If you are not using a form, then with an ActiveX Combo Box inserted on your worksheet, you would have to enter the range of names in the Properties Window of the Combo Box. After you insert the ComboBox on your worksheet, right click and select properties. Put your range in the highlighted row. Again, this code would have be in the worksheet module.
ComboBox1.png
Then the following code would work

VBA Code:
Private Sub ComboBox1_Click()
    
    Dim ws As Worksheet: Set ws = Worksheets("Sheet1")
    Dim tbl As Object, cbo As Object
    Dim nam As String, nte As String

    Set cbo = Worksheets("Sheet1").ComboBox1
    Set tbl = ws.ListObjects("Table1")
    
    ActiveSheet.ListObjects("Table1").ListRows.Add AlwaysInsert:=True
    nam = cbo.Value
    nte = Application.InputBox("Input Note for " & nam & " on " & Date, , , , , , , 2)
    tbl.DataBodyRange(tbl.ListRows.Count, 1) = Date
    tbl.DataBodyRange(tbl.ListRows.Count, 2) = nam
    tbl.DataBodyRange(tbl.ListRows.Count, 3) = nte

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top