Adding 30 rows of new data to an Excel Table using VBA

I have created a procedure to add 30-40 rows (variable number) of data to an existing excel Table.  The procedure needs to transfer the data from one sheet (Import) to another sheet (Database).  The Database sheet is a formatted excel table and the new data needs to be added to the bottom (first available blank row).  I would like help making this copy process consistent and error free. [Note that the date that is copied is formula driven so it needs to be paste special values or directly referenced.

If doing a simple copy and paste the data is transferred but there are warnings about shifting cells below the table down.  When i turned off alerts i had more success but occasional code errors such as incorrect referencing of the table list object.  I found alternative methods where the table was resized using VBA but this also gave occasional errors/behaviour is not consistent.  Any suggestions will be welcomed.
Add-Rows-to-Table-with-VBA.xlsm
JohnNZExcelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
Sheet9.Range("B6:Z" & 5 + copyrows - 1).Copy

Set tbl = ActiveSheet.ListObjects("Database")
tbl.Resize tbl.Range.Resize(tbl.Range.Rows.Count + copyrows - 1)

Cells(firstcopiedrow, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False

Open in new window

Regards
0
Saurabh Singh TeotiaCommented:
This code works for me without giving an error...

Sub copydata()
    Dim lrow As Long
    Dim lr As Long
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("Import")
    Set ws1 = Sheets("Database")

    lrow = ws.Cells(Cells.Rows.Count, "B").End(xlUp).Row
    ws.Range("B6:Z" & lrow).Copy

    lr = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1
    ws1.Range("A" & lr).PasteSpecial xlPasteValuesAndNumberFormats

    Application.CutCopyMode = False


    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnNZExcelAuthor Commented:
Thanks for the contributions - my question was not thorough enough it seems.  I actually had several columns of formula also in the table and the interaction the VBA had with those formula when new rows were added was the root of the problem.  Adding data with either of the two methods suggested was the easy bit but then excel would be confused as to how to update the table formula and i would get inconsistent error messages.  I have since removed the table format and simply used a dynamic range.  Problem solved.  Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.