VBA adding values to new table rows

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
83
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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