VBA for Userform to add data to a Table

tigerdel

Board Regular
Joined
Oct 13, 2015
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I have a UserForm [frmAddNewCustomer] and I am trying to get the data entered into a Table [Customers] on Sheet [Customer List] in the next row in the Table when the user clicks Add

1614439610555.png


The code is:
Code:
Private Sub cmbAdd_Click()
Dim lastRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Customer List")
With ws.ListObjects("Customers")
          .ListColumns("Company Name").Range(lastRow) = txtCust.Value
          .ListColumns("Address Line 1").Range(lastRow) = txtAddress1.Value
          .ListColumns("Address Line 2").Range(lastRow) = txtAddress2.Value
          .ListColumns("Address Line 3").Range(lastRow) = txtAddress3.Value
          .ListColumns("Town").Range(lastRow) = txtTown.Value
          .ListColumns("County").Range(lastRow) = txtCounty.Value
          .ListColumns("Post Code").Range(lastRow) = txtPostCode.Value
          .ListColumns("Name").Range(lastRow) = txtCustomerName.Value
          .ListColumns("Phone").Range(lastRow) = txtPhone.Value
          .ListColumns("email address").Range(lastRow) = txtemail.Value
Unload Me
End With
End Sub

However, when I run the UserForm I don't get any error but nothing is placed in the table

Any help gratefully received
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How about like
VBA Code:
Private Sub cmbAdd_Click()
Dim NewRow As ListRow
Dim ws As Worksheet
Set ws = Worksheets("Customer List")
With ws.ListObjects("Customers")
        With .ListRows.Add
          .Range(.Parent.ListColumns("Company Name").Index) = txtCust.Value
          .Range(.Parent.ListColumns("Address Line 1").Index) = txtAddress1.Value
'          .ListColumns("Address Line 2").Range(lastRow) = txtAddress2.Value
'          .ListColumns("Address Line 3").Range(lastRow) = txtAddress3.Value
'          .ListColumns("Town").Range(lastRow) = txtTown.Value
'          .ListColumns("County").Range(lastRow) = txtCounty.Value
'          .ListColumns("Post Code").Range(lastRow) = txtPostCode.Value
'          .ListColumns("Name").Range(lastRow) = txtCustomerName.Value
'          .ListColumns("Phone").Range(lastRow) = txtPhone.Value
'          .ListColumns("email address").Range(lastRow) = txtemail.Value
Unload Me
End With
End Sub
 
Upvote 0
Solution
Hi,
untested but try

VBA Code:
Private Sub CmbAdd_Click()
    Dim NewRecord As ListRow
    Dim wsCustomerList As Worksheet
    Dim tblCustomers As ListObject
    
    Set wsCustomerList = ThisWorkbook.Worksheets("Customer List")
    Set tblCustomers = wsCustomerList.ListObjects("Customers")
    
    'get next table row
    Set NewRecord = tblCustomers.ListRows.Add(AlwaysInsert:=True)

    'Add Data
    With NewRecord
        .Range(1).Value = txtCust.Value
        .Range(2).Value = txtAddress1.Value
        .Range(3).Value = txtAddress2.Value
        .Range(4).Value = txtAddress3.Value
        .Range(5).Value = txtTown.Value
        .Range(6).Value = txtCounty.Value
        .Range(7).Value = txtPostCode.Value
        .Range(8).Value = txtCustomerName.Value
        .Range(9).Value = txtPhone.Value
        .Range(10).Value = txtemail.Value
    End With
    
End Sub

change the ranges where each textbox posts it data as required

Dave
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,798
Messages
6,121,636
Members
449,043
Latest member
farhansadik

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