Need to Run Subtotal formula Top of the Column and run till end available data

NasimAnsari

New Member
Joined
Sep 2, 2020
Messages
19
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello Everyone,

I would need help with the Subtotal formula to run the top column till end data with currency formula for specific columns.
Currently, I am using the below code which is not helping me

VBA Code:
Sub Subtotal ()
Dim Lastrow As Long
Dim LastCol As Long
With Worksheets("Trending Report")
     Lastrow = .Cells(Rows.Count, "L").End(xlUp).Row
    .Range("L3:L" & Lastrow).Select
LastCol = ActiveSheet.Range("L3").End(xlToLeft).Column
ActiveSheet.Range("L3", ActiveSheet.Cells(Lastrow, LastCol)).Select
End With
With Selection
ActiveSheet.Range("L3", ActiveSheet.Cells(Lastrow, LastCol)).Select
Range("L3").Select
ActiveCell.FormulaR1C1 = "=SUBTOTAL(9,R[2]C:R[999998]C)"
Selection.Copy
Range("M3:P3").Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Range("L3:P3").Select
Selection.Font.Bold = True
Selection.Interior.Color = RGB(219, 219, 219)
Selection.Font.Bold = True
Selection.Font.Size = 12
    With Selection
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
With Selection.Font
        .ColorIndex = xlAutomatic
    End With
    Range("L3").Select
End With
End With
 

Attachments

  • Subtotal.png
    Subtotal.png
    66.4 KB · Views: 21
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hi & welcome to MrExcel.
Do you just want the formula in L3:P3, or should it be to the last col?
 
Upvote 0
Hi & welcome to MrExcel.
Do you just want the formula in L3:P3, or should it be to the last col?

Yes, I would need VBA code to run my subtotal formula till data end.
I would be great help for me.
 
Upvote 0
Ok, how about
VBA Code:
Sub NasimAnsari()
   Dim Lastrow As Long
   Dim LastCol As Long
   With WorkSheets("Trending Report")
      Lastrow = .Cells(Rows.Count, "K").End(xlUp).Row
      LastCol = .Cells(4, Columns.Count).End(xlToLeft).Column
      .Range("L3", .Cells(3, LastCol)).FormulaR1C1 = "=SUBTOTAL(9,R5C:R" & Lastrow & "C)"
      .NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
      .Font.Bold = True
      .Interior.Color = RGB(219, 219, 219)
      .Font.Size = 12
      .HorizontalAlignment = xlGeneral
      .VerticalAlignment = xlCenter
      .WrapText = False
      .Orientation = 0
      .AddIndent = False
      .IndentLevel = 0
      .ShrinkToFit = False
      .ReadingOrder = xlContext
      .MergeCells = False
      .Font.ColorIndex = xlAutomatic
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub NasimAnsari()
   Dim Lastrow As Long
   Dim LastCol As Long
   With WorkSheets("Trending Report")
      Lastrow = .Cells(Rows.Count, "K").End(xlUp).Row
      LastCol = .Cells(4, Columns.Count).End(xlToLeft).Column
      .Range("L3", .Cells(3, LastCol)).FormulaR1C1 = "=SUBTOTAL(9,R5C:R" & Lastrow & "C)"
      .NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
      .Font.Bold = True
      .Interior.Color = RGB(219, 219, 219)
      .Font.Size = 12
      .HorizontalAlignment = xlGeneral
      .VerticalAlignment = xlCenter
      .WrapText = False
      .Orientation = 0
      .AddIndent = False
      .IndentLevel = 0
      .ShrinkToFit = False
      .ReadingOrder = xlContext
      .MergeCells = False
      .Font.ColorIndex = xlAutomatic
   End With
End Sub

it's Working perfectly You are great Man... I must appreciate your work.
Thanks, Fluff and have a good day.?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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