Consulting

Results 1 to 4 of 4

Thread: Inset formula into last cell of table column

  1. #1
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    2
    Location

    Inset formula into last cell of table column

    I have a table called Table1 on Sheet1 of an Excel workbook which can contain any number of rows. I want to add a row below the last row of the table, which I am doing with:

    Sub insertRow()
     
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim tbl As ListObject
    Set tbl = ws.ListObjects("Table1")
    tbl.ListRows.Add
    I now want to add SUM functions to the new last cell of 3 Table1 columns (Quantity, Price, Cost).
    I would appreciate it if someone could give me some guidance on how to achieve this.

    Finally, I would like to reference these SUM values from Sheet2 e.g. place the Sum of Table1 column Cost into Cell A1 on Sheet2.
    Again, any help with this would be greatly appreciated.

    TIA

  2. #2
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi Si2873!
    Welcome to vbax forum.
    If you can upload an attachment, it is estimated that the problem will be solved soon.

  3. #3
    VBAX Newbie
    Joined
    Jul 2019
    Posts
    2
    Location
    Quote Originally Posted by 大灰狼1976 View Post
    Hi Si2873!
    Welcome to vbax forum.
    If you can upload an attachment, it is estimated that the problem will be solved soon.

    Hi,

    Thank you for taking the time to reply.

    VBA example.xlsm

    In the attached example Sheet1 and Sheet2 are what I am starting with and Sheet3 and Sheet4 are examples of which I want Sheet1 and Sheet2 to look like at the end.

    Any help or guidance would be greatly appreciated.

  4. #4
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    Sub Makro4()
    
        With Worksheets("Sheet1").ListObjects(1)
            .ShowTotals = True
            .ListColumns("Quantity").TotalsCalculation = xlTotalsCalculationSum
            .ListColumns("Price").TotalsCalculation = xlTotalsCalculationSum
            .ListColumns("Cost").TotalsCalculation = xlTotalsCalculationSum
            .ListColumns("Date").TotalsCalculation = xlTotalsCalculationNone
        End With
    
    
        With Worksheets("Sheet2").ListObjects(1).ListRows(1).Range
            .Cells(1).FormulaR1C1 = "=Table1[[#Totals],[Quantity]]"
            .Cells(2).FormulaR1C1 = "=Table1[[#Totals],[Price]]"
            .Cells(3).FormulaR1C1 = "=Table1[[#Totals],[Cost]]"
        End With
    End Sub
    Artik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •