Add a new row in Named Table (Table1) and add data from txtboxes into last empty cells based on header named of the Table1

karl2022

New Member
Joined
Sep 24, 2022
Messages
8
Office Version
  1. 2011
Platform
  1. Windows
Hello All,

I would need your expertise, please.

I have a named Table1 with columns test1, test2, test3, test4 and textbox1,textbox2,textbox3,textbox4
Using click procedure, I would like to create a VBA to add a new row in Table1 and based on with header name of the Table1, the data to be populated from textboxes, so for example, if i want to upload data from textbox1, the code will search in table1 the header test2,then the last empty cell and populate the data in cell
I tried but didnt work.:(...Anyone can guide me on this, please?

Dim col As Long
Dim Row As Long
col = ActiveSheet.ListObjects("Table1").ListColumns("test2").Range.Column
Row = Cells(Rows.Count, col).End(xlUp).Row
Cells(FirstEmptyRow + 1, col) = Me.textbox1.Value
 

Attachments

  • err.JPG
    err.JPG
    16.2 KB · Views: 12

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this.
Put the code in your userform.
I suppose you have a button named "CommandButton1" in the userform.
When you press the button, trigger the code.
And surely, you have a table named "Table1" in the ActiveSheet.
VBA Code:
Private Sub CommandButton1_Click()
    Dim myTable As ListObject
    Set myTable = ActiveSheet.ListObjects("Table1")
    With myTable
        .ListRows.Add
        Dim myRowsCount
        myRowsCount = .ListRows.Count + 1
        .Range(myRowsCount, 1) = TextBox1
        .Range(myRowsCount, 2) = TextBox2
        .Range(myRowsCount, 3) = TextBox3
        .Range(myRowsCount, 4) = TextBox4
    End With
End Sub
 
Upvote 0
Solution
Hello HongRu,

Much appreciated your time on this and thanks for replying.
The idea was that in case others columns will be added between those existed columns, the above code has to be revised. This why I tried to use the header column, so the code will add a line, then search/find the column header of the named table linked to textbox and add the value in the last row....
Any additional thoughts, please?
Thank you
 
Upvote 0
Hello, somehow I am still getting this error. Is this because i have several named tables in activesheet?
1670843608921.png
 
Upvote 0
You might need to show us all the code you are using and contains the Add row line, so we can try and figure out the error.

In terms of using the Column heading you should be able to replace the lines that look like this in HongRu's code
VBA Code:
        .Range(myRowsCount, 1) = TextBox1

With this format:
Rich (BB code):
.ListColumns("test2").Range(myRowsCount) = TextBox1
 
Upvote 0
You might need to show us all the code you are using and contains the Add row line, so we can try and figure out the error.

In terms of using the Column heading you should be able to replace the lines that look like this in HongRu's code
VBA Code:
        .Range(myRowsCount, 1) = TextBox1

With this format:
Rich (BB code):
.ListColumns("test2").Range(myRowsCount) = TextBox1
Thanks Alex. much appreciated. i used your suggestion in HongRu code. It works perfectly. It seems that the error message was caused by using same range with rowsource for Listbox and combobox and when listbox was updated, the code for combobox was crashing my file, so i used array :)

THANKS ALOT
 
Upvote 0
Thanks again, unfortunately, i am not able/allowed to mark 2 solutions, apologizes :( .
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,077
Latest member
Jocksteriom

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