Copying data from a textbox to the bottom of a table

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,373
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a table and two text boxes and I want to be able to write a name and a price in them, then press a button and copy them to a table on another sheet. The table name is: Services.

I started with this code, I thought I needed to dim a few variables to hold the values of the text boxes first so I started with this code:

Code:
Private Sub cmdAddNewService_Click()
    Dim NewName As String
    Dim NewPrice As String
    
NewName = txtName.Value
NewPrice = txtPrice.Value


End Sub

I know that there is more code needed but I didn't know how to write it. The table is called ServicePrices and the column names are Description and Unit_Price. I have two text boxes that I want be able to enter a new service and the price and add it to the bottom of the table. Could someone help me with the extra code please?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You said:
The table name is: Services.

But then said:
The table is called ServicePrices

Well my script assumes the Table name is
ServicePrices

You said Table is on another sheet but did not name the sheet.
My script assumes the sheet name is "Help"

I also assume
Name goes in column 1 of Table and price goes in column 2 of Table

If these assumptions are not correct modify the script

See when asking for help we always need specific details.

And when you say Table I assume you mean A Excel Table and not a Excel sheet.

Try this:

Code:
Private Sub CommandButton1_Click()
'Modified  12/18/2018  11:24:48 PM  EST
Dim ans As Long
With Sheets("Help").ListObjects("ServicePrices").DataBodyRange
    ans = .Rows.Count + 1
    .Cells(ans, 1).Value = txtName.Value
    .Cells(ans, 2).Value = txtPrice.Value
End With
End Sub

 
Upvote 0
Thanks, that is perfect.

Sorry I forgot to specify the correct names etc.

I now need a way to delete services. I was going to add a combo box which would allow a selection of a service. I then was going to add a button to delete the record of the service name selected. What settings would I need to change on the combo box and what code would i need to delete the record.

The table name where the data is kept is called ServicePrices, the sheet name where the table is kept is called Data. Hope I have included everything.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,710
Messages
6,174,019
Members
452,542
Latest member
Bricklin

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