Macro help with TotalsCalculation

pvzshark

Repeated rules violations
Joined
Feb 26, 2020
Messages
23
Office Version
  1. 2013
Platform
  1. Windows
ActiveSheet.ListObjects("Table_pastel_12_cust_rank").ListColumns("2019-032"). _
TotalsCalculation = xlTotalsCalculationSum


Hi there
below is my macrofrom a sheet where i import data from mysql database. the field names are not always the same and keeps changing. thus above example will not help me.

I need something like to make the calculation - show totals auto sum for column c.

ActiveSheet.ListObjects("$C").ListColumns("$C"). _
TotalsCalculation = xlTotalsCalculationSum

VBA Code:
Sub Accounts_macro()
'
' Accounts_macro Macro
'

'
    Columns("C:CE").Select
    Selection.Delete Shift:=xlToLeft
    Range("B4").Select
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    Columns("E:E").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "=R[-2]C[-1]-R[-2]C[-2]"
    Range("F4").Select
    Columns("F:F").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("F4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("E4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("H:H").Select
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("I4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("J:J").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("J4").Select
    ActiveCell.FormulaR1C1 = "(h4-g4)/h4"
    Range("J4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("J5").Select
    Columns("J:J").ColumnWidth = 8.86
    Columns("M:M").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("M4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Range("N4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("N5").Select
    ActiveWindow.SmallScroll ToRight:=1
    Columns("P:P").EntireColumn.AutoFit
    Range("N5").Select
    Selection.AutoFill Destination:=Range("N4:N5"), Type:=xlFillDefault
    Range("N4:N5").Select
    Columns("N:N").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("N4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Columns("Q:Q").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Q4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("R:R").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("R4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("R5").Select
    ActiveWindow.ScrollColumn = 4
    ActiveWindow.ScrollColumn = 5
    ActiveWindow.ScrollColumn = 6
    ActiveWindow.ScrollColumn = 7
    ActiveWindow.ScrollColumn = 8
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 10
    ActiveWindow.ScrollColumn = 11
    ActiveWindow.ScrollColumn = 12
    ActiveWindow.ScrollColumn = 13
    ActiveWindow.ScrollColumn = 14
    ActiveWindow.ScrollColumn = 15
    ActiveWindow.ScrollColumn = 16
    ActiveWindow.ScrollColumn = 17
    Columns("U:U").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("U4").Select
    Columns("T:T").EntireColumn.AutoFit
    Columns("S:S").EntireColumn.AutoFit
    Range("U4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Range("V4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Columns("Y:Y").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Y4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("Z:Z").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Z4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Columns("AC:AC").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AC4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("AD:AD").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AD4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("AD5").Select
    ActiveWindow.SmallScroll ToRight:=6
    Columns("AG:AG").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AG4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Range("AH4").Select
    Columns("AH:AH").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AH4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Columns("AK:AK").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AK4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("AL:AL").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AL4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("AL5").Select
    ActiveWindow.SmallScroll ToRight:=7
    Columns("AO:AO").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AO4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("AP:AP").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AP4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-3]"
    Columns("AS:AS").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AS4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("AT:AT").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("AT4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-2]"
    Range("AT5").Select
    ActiveWindow.SmallScroll ToRight:=6
    Range("Table_pastel_12_cust_rank[[#Headers],[2019-02]]").Select
    
    Range("AV5").Select
    ActiveWindow.SmallScroll ToRight:=7
    Columns("Aw:Aw").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Aw4").Select
    ActiveCell.FormulaR1C1 = "=RC[-1]-RC[-2]"
    Columns("Ax:Ax").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("Ax4").Select
    ActiveCell.FormulaR1C1 = "=(RC[-2]-RC[-3])/RC[-3]"
      Range("AY4").Select
    ActiveCell.FormulaR1C1 = _
        "=RC[-48]+RC[-44]+RC[-40]+RC[-36]+RC[-32]+RC[-28]+RC[-24]+RC[-20]+RC[-16]+RC[-12]+RC[-8]+RC[-4]"
    Range("AZ4").Select
    ActiveCell.FormulaR1C1 = _
        "=RC[-48]+RC[-40]+RC[-36]+RC[-32]+RC[-28]+RC[-24]+RC[-20]+RC[-16]+RC[-12]+RC[-8]+RC[-4]"

    
   
    
    Range("Table_pastel_12_cust_rank[[#Totals],[2019-033]]").Select
    ActiveWindow.ScrollColumn = 2
    ActiveWindow.ScrollColumn = 20
    ActiveWindow.ScrollColumn = 31
    ActiveWindow.ScrollColumn = 39
    ActiveWindow.ScrollColumn = 43
    ActiveWindow.ScrollColumn = 52
    ActiveWindow.ScrollColumn = 54
    ActiveWindow.ScrollColumn = 57
    ActiveWindow.ScrollColumn = 71
    ActiveWindow.ScrollColumn = 73
    ActiveWindow.ScrollColumn = 72
    ActiveWindow.ScrollColumn = 70
    ActiveWindow.ScrollColumn = 68
    ActiveWindow.ScrollColumn = 65
    ActiveWindow.ScrollColumn = 56
    ActiveWindow.ScrollColumn = 54
    ActiveWindow.ScrollColumn = 53
    ActiveWindow.ScrollColumn = 49
    ActiveWindow.ScrollColumn = 48
    ActiveWindow.ScrollColumn = 47
    ActiveWindow.ScrollColumn = 45
    ActiveWindow.ScrollColumn = 44
    ActiveWindow.ScrollColumn = 43
    ActiveWindow.ScrollColumn = 42
    ActiveWindow.ScrollColumn = 41
    ActiveWindow.ScrollColumn = 42
       
    ActiveSheet.ListObjects("Table_pastel_12_cust_rank").ShowTotals = False
    ActiveSheet.ListObjects("Table_pastel_12_cust_rank").ShowTotals = True
             
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Cross posted help with xlTotalsCalculationSum - OzGrid Free Excel/VBA Help Forum
and

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Figured this out-
lastrow = Cells(Rows.Count, 54).End(xlUp).Row
Cells(lastrow, 54).Formula = "=IF(AND(az" & lastrow & "=0,ay" & lastrow & "=0),0,IF(AND(az" & lastrow & ">0,ay" & lastrow & "=0),-1,IF(AND(az" & lastrow & "=0,ay" & lastrow & ">0),1,IF(AND(az" & lastrow & ">0,ay" & lastrow & ">0),(az" & lastrow & "-ay" & lastrow & ")/ay" & lastrow & "))))"
 
Upvote 0
We do not mark threads as solved here. You've posted your solution & said it's sorted, so all's good.
 
Upvote 0

Forum statistics

Threads
1,213,558
Messages
6,114,296
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top