Add Data to table

Palacemad

New Member
Joined
May 19, 2019
Messages
41
Hi.

I am trying to add data from one worksheet to a table in another worksheet. I have so far come up with the following code, but this is causing the data to sit underneath the table, rather than be added to the next empty row. Can someone please help to guide me as to how I identify the table itself? I have tried replacing the dst as ListObject and then identify the ListObject but that keeps returning a fault each time. The name of the table is "Reg_BetTable" in the Worksheet "Regular Bets"


Thank you in advance for any guidance.

Sub Add_Data_RegBets()


Dim src As Worksheet
Dim dst As Worksheet
Dim rw As Long

Application.ScreenUpdating = False

' Set source and destination sheets
Set src = Sheets("Calculator - Regular")
Set dst = Sheets("Regular Bets")

' Find next available row on destination sheet
rw = dst.Cells(Rows.Count, "C").End(xlUp).Row + 1

' Populate values on destination sheet
dst.Cells(rw, "C") = src.Range("D7")
dst.Cells(rw, "D") = src.Range("D8")
dst.Cells(rw, "E") = src.Range("D11")
dst.Cells(rw, "F") = src.Range("D10")
dst.Cells(rw, "G") = src.Range("G7")
dst.Cells(rw, "H") = src.Range("D12")
dst.Cells(rw, "I") = src.Range("D9")
dst.Cells(rw, "J") = src.Range("G8")
dst.Cells(rw, "K") = src.Range("G9")
dst.Cells(rw, "L") = src.Range("G12")

Application.ScreenUpdating = True




End Sub
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

theBardd

Rules violation
Joined
Jan 21, 2012
Messages
912
Try this

Code:
Sub Add_Data_RegBets()
Dim src As Worksheet
Dim dst As Worksheet
Dim rw As Long
    
    Application.ScreenUpdating = False
    
    ' Set source and destination sheets
    Set src = Worksheets("Calculator - Regular")
    Set dst = Worksheets("Regular Bets")
    
    With dst.ListObjects("Reg_BetTable")
    
        .ListRows.Add
        ' Find next available row in destination table
        rw = .DataBodyRange.Rows.Count
    
         ' Populate values on destination sheet
        .DataBodyRange.Cells(rw, 1) = src.Range("D7")
        .DataBodyRange.Cells(rw, 2) = src.Range("D8")
        .DataBodyRange.Cells(rw, 3) = src.Range("D11")
        .DataBodyRange.Cells(rw, 4) = src.Range("D10")
        .DataBodyRange.Cells(rw, 5) = src.Range("G7")
        .DataBodyRange.Cells(rw, 6) = src.Range("D12")
        .DataBodyRange.Cells(rw, 7) = src.Range("D9")
        .DataBodyRange.Cells(rw, 8) = src.Range("G8")
        .DataBodyRange.Cells(rw, 9) = src.Range("G9")
        .DataBodyRange.Cells(rw, 10) = src.Range("G12")
    End With
    
    Application.ScreenUpdating = True
End Sub
 

Palacemad

New Member
Joined
May 19, 2019
Messages
41
Thank you very much! That generally works, looks like it was just the one line of code that was missing.

The only issue, and it is very minor and not a major problem is that it is not filling out the first row of the table, it is immediately going to the row below. It then seems to work properly. I'm not really that bothered, more intrigued as to why it is doing that.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,378
Messages
5,486,519
Members
407,550
Latest member
LucasBordure

This Week's Hot Topics

Top