Tocogroup
asked on
How do I add a new row to an Excel table using VBA ?
Hi,
I have an Excel 2010 table, named Sales', to which I want to append a new row in VBA. Do I use the standard End(xlUp).Row construct for normal sheets, or is there a different method of determining the last row in a table ?
Can I reference the table by name, or do I have to refer to the sheet name ?
Thanks
Toco
I have an Excel 2010 table, named Sales', to which I want to append a new row in VBA. Do I use the standard End(xlUp).Row construct for normal sheets, or is there a different method of determining the last row in a table ?
Can I reference the table by name, or do I have to refer to the sheet name ?
Thanks
Toco
ASKER
Hi,
I'm getting a Run Time error 438 'Object doesn't support this method or property' on your first line of code ,,,,,activesheet.ListObjec ts(1).List Rows.Count
Any reason why ?
Thanks
Toco
I'm getting a Run Time error 438 'Object doesn't support this method or property' on your first line of code ,,,,,activesheet.ListObjec
Any reason why ?
Thanks
Toco
can you post your code ?
gowflow
gowflow
ASKER
PLEASE FIND CODE BELOW
I've only introduced the first line of your code, marked with asterisks.....*****
Private Sub Add_Sales()
' Declare the variables of the 3 worksheets
Dim wsSales As Worksheet
Dim wsSalesDetails As Worksheet
Dim wsTemplate As Worksheet
' Declare variables for the last data row of Sales and Sales Details sheets
Dim lastRowSales As Long
Dim lastRowSalesDetails As Long
' Declare variables for template cells
Dim InvoiceNo As Long
Dim InvoiceDate As Date
' Set each sheet
Set wsSales = ThisWorkbook.Sheets("Sales ")
Set wsSalesDetails = ThisWorkbook.Sheets("Sales Details")
Set wsTemplate = ThisWorkbook.Sheets("Templ ate")
' Set the last row variables
lastRowSales = wsSales.Range("A65536").En d(xlUp).Ro w
lastRowSalesDetails = wsSales.Range("A65536").En d(xlUp).Ro w
MsgBox (lastRowSales)
***** ERROR OCCURS HERE
ActiveSheet.ListObjects("S ales").Lis tRows
*****
' Add new Sales row to end of range
wsSales.Rows(lastRowSales) .Copy
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteFormats
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteFormulas
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteValidation
wsSales.Rows(lastRowSales + 1).SpecialCells(xlCellType Constants, 23).ClearContents
On Error Resume Next
InvoiceNo = Worksheets("Tables").Cells (2, 6)
wsSales.Range("a" & lastRowSales + 1).Value = InvoiceNo
' Increment the Next Invoice number
Worksheets("Tables").Cells (2, 6) = InvoiceNo + 1
With Application
.ScreenUpdating = True
.CutCopyMode = False
End With
wsSales.Range("B" & lastRowSales + 1).Select
MsgBox ("Please enter the Sales details")
' Populate the default sales details
Worksheets("Template").Sel ect
wsTemplate.Range("G9").Val ue = InvoiceNo
InvoiceDate = Date
wsTemplate.Range("G10").Va lue = InvoiceDate
End Sub
I've only introduced the first line of your code, marked with asterisks.....*****
Private Sub Add_Sales()
' Declare the variables of the 3 worksheets
Dim wsSales As Worksheet
Dim wsSalesDetails As Worksheet
Dim wsTemplate As Worksheet
' Declare variables for the last data row of Sales and Sales Details sheets
Dim lastRowSales As Long
Dim lastRowSalesDetails As Long
' Declare variables for template cells
Dim InvoiceNo As Long
Dim InvoiceDate As Date
' Set each sheet
Set wsSales = ThisWorkbook.Sheets("Sales
Set wsSalesDetails = ThisWorkbook.Sheets("Sales
Set wsTemplate = ThisWorkbook.Sheets("Templ
' Set the last row variables
lastRowSales = wsSales.Range("A65536").En
lastRowSalesDetails = wsSales.Range("A65536").En
MsgBox (lastRowSales)
***** ERROR OCCURS HERE
ActiveSheet.ListObjects("S
*****
' Add new Sales row to end of range
wsSales.Rows(lastRowSales)
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteFormats
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteFormulas
wsSales.Range("a" & lastRowSales + 1).PasteSpecial xlPasteValidation
wsSales.Rows(lastRowSales + 1).SpecialCells(xlCellType
On Error Resume Next
InvoiceNo = Worksheets("Tables").Cells
wsSales.Range("a" & lastRowSales + 1).Value = InvoiceNo
' Increment the Next Invoice number
Worksheets("Tables").Cells
With Application
.ScreenUpdating = True
.CutCopyMode = False
End With
wsSales.Range("B" & lastRowSales + 1).Select
MsgBox ("Please enter the Sales details")
' Populate the default sales details
Worksheets("Template").Sel
wsTemplate.Range("G9").Val
InvoiceDate = Date
wsTemplate.Range("G10").Va
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi goflow,
I identified the error in my code as pointed out by yourself. Your code now works fine.
Many thanks for your help
Phil
I identified the error in my code as pointed out by yourself. Your code now works fine.
Many thanks for your help
Phil
your welcome tks for the grade.
gowflow
gowflow
activesheet.ListObjects(1)
you get the number of rows in the table
Now you can call the table if you know the name
activesheet.ListObjects("T
Now to add a row
activesheet.ListObjects("T
will add a row at the end of the table
if you want to add a row at the 5th row
activesheet.ListObjects("T
same for the columns
activesheet.ListObjects("T
will add a column at the right most of the edget of the table
activesheet.ListObjects("T
Hope hv answered your quesitons. Pls feel free to revert if not clear or if you have a practical example you need help with.
gowflow