VBA Code for Dynamic Total

Nasmin Saheed

New Member
Joined
Jun 11, 2015
Messages
49
Office Version
  1. 365
Platform
  1. Windows
Hi

I have a dynamic field for get total in my working sheet, Total column is same but row can be variable depending on description include

Example cart is here

S.NoDescriptionCodeCountry CtnQtyPriceAmountVAT %VAT AmountTotal AmountGross Weight
(Kgs)
Net Weight
(Kgs)
1Item Number - 11652India21015.2812 152.812 - - 152.81265
2Item Number - 21234Korea21015.3511 153.511 - - 153.51165
3Item Number - 31234Japan21015.2441 152.441 - - 152.44165
-
GRAND TOTAL630 458.764 - 458.764 1815

<tbody>
</tbody>

I have applied VBA code to get the total as below, total is calculating correctly but once macro is run , if I press F2 can not see the formula, and also I need to get the total with 2 decimal (here I need to apply round formula, I don’t want to apply comma style , )

My coding is below , could you please someone correct me with the correct code what exactly I wanted


Note:- G (column 7) is Ctn
H (column 8) is Qty
J (column 10) is Amount
Etc........

Sheets("Invoice").Range("G" & Sheets("Invoice").Cells(Rows.Count, 7).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("G2:G" & Sheets("Invoice").Cells(Rows.Count, 7).End(xlUp).Row))
Sheets("Invoice").Range("H" & Sheets("Invoice").Cells(Rows.Count, 8).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("H2:H" & Sheets("Invoice").Cells(Rows.Count, 8).End(xlUp).Row))
Sheets("Invoice").Range("J" & Sheets("Invoice").Cells(Rows.Count, 10).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("J2:J" & Sheets("Invoice").Cells(Rows.Count, 10).End(xlUp).Row))
Sheets("Invoice").Range("L" & Sheets("Invoice").Cells(Rows.Count, 12).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("L2:L" & Sheets("Invoice").Cells(Rows.Count, 12).End(xlUp).Row))
Sheets("Invoice").Range("M" & Sheets("Invoice").Cells(Rows.Count, 13).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("M2:M" & Sheets("Invoice").Cells(Rows.Count, 13).End(xlUp).Row))
Sheets("Invoice").Range("N" & Sheets("Invoice").Cells(Rows.Count, 14).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("N2:N" & Sheets("Invoice").Cells(Rows.Count, 14).End(xlUp).Row))
Sheets("Invoice").Range("O" & Sheets("Invoice").Cells(Rows.Count, 15).End(xlUp).Row + 2) = Application.WorksheetFunction.Sum(Sheets("Invoice").Range("O2:O" & Sheets("Invoice").Cells(Rows.Count, 15).End(xlUp).Row))
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about like
Code:
With Sheets("Invoice")
   .Range("G" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=sum(r2c:r[-2]c)"
   .Range("H" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=sum(r2c:r[-2]c)"
   .Range("J" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=round(sum(r2c:r[-2]c),2)"
End With
 
Upvote 0
How about like
Code:
With Sheets("Invoice")
   .Range("G" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=sum(r2c:r[-2]c)"
   .Range("H" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=sum(r2c:r[-2]c)"
   .Range("J" & Rows.Count).End(xlUp).Offset(2).FormulaR1C1 = "=round(sum(r2c:r[-2]c),2)"
End With


Hi Sir,

Thanks a lots it is works for me ...
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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