VBA adding values to new table rows

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
92
Office Version
  1. 365
Platform
  1. MacOS
This code is working perfectly for adding values to one column in NEW table rows, But I wanted to add a 2nd value in the same newly created table row. But my syntax is bad- can you help me?

VBA Code:
' Add RECORD ROW to table

Sub AddRecordToTable()

    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

        With ws.ListObjects("tbl_BANKS")
            .ListRows.Add(1).Range(.ListColumns("ID").Index) = GUID
        
            ' How do I add a 2nd value such as: Also add the value "Smith" in the column "LastName"
        
        
        End With


End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try the following...

VBA Code:
' Add RECORD ROW to table

Sub AddRecordToTable()

    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

    With ws.ListObjects("tbl_BANKS")
        Set newRow = .ListRows.Add(1)
        newRow.Range(.ListColumns("ID").Index) = GUID
        newRow.Range(.ListColumns("LastName").Index) = "Smith"
    End With

End Sub

Hope this helps!
 
Upvote 0
You're very welcome, glad I could help.

Cheers!
Oops when I started using it in my solution it is creating the new rows at the bottom of the table instead the top. Is there an easy way to modify the code to that the new row is at the top?
 
Upvote 0
Oops when I started using it in my solution it is creating the new rows at the bottom of the table instead the top. Is there an easy way to modify the code to that the new row is at the top?
It works in my my test sheet but when I added that to my real solution (which combines other rules) it doesn't work. Thats on me - I'll figure it out. Thanks for your help though!
 
Upvote 0
Are you still having problems? If so, are you getting an error? And if so, which error, and on which line?
Oops when I started using it in my solution it is creating the new rows at the bottom of the table instead the top. Is there an easy way to modify the code to that the new row is at the top?
The first argument of the Add method of the ListRows object specifies the position at which the new row should be inserted relative to the first/top row. So if you've set that first argument to 1, as per your example, it should do so. Did you?
 
Upvote 0
It works on my test workbook. But when I combine it with 7 other subs on the same sheet weirdness ensues. I will need to have someone eventually optimize my entire solution so that things are clean and efficient
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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