• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 128
  • Last Modified:

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
0
JohnNZExcel
Asked:
JohnNZExcel
2 Solutions
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now