Total column using last active cell

johndrew

Board Regular
Joined
Apr 21, 2007
Messages
100
I have the following code for totaling a column.

Code:
Dim LR As Long
With Sheets("inventory")
    LR = .Range("e" & Rows.Count).End(xlUp).Row
        .Range("e" & LR + 1).Value = WorksheetFunction.Sum(.Range("e1:e" & LR))
End With

The problem is it finds the previous total and places a new total just below it. What I need is to delete the old total and replace with the new total.
 

Some videos you may like

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,)

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,764
Office Version
365
Platform
Windows
Can you not just remove +1?
 

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,600
LR is the row your old total is on but in your code you're using "LR + 1". Edit your "LR="-row by adding "-1" to it and your code should do what you're after.
 

johndrew

Board Regular
Joined
Apr 21, 2007
Messages
100
In E1 thru E5, I have then number (1) in each cell.
When I run the macro, I get 5 in E4
If I run again, I get 9 in E4
This is using the -1
 

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,600
Where did you put the -1 to? Here's the code as it should be:
Code:
Dim LR As Long
With Sheets("inventory")
    LR = .Range("e" & Rows.Count).End(xlUp).Row - 1
        .Range("e" & LR + 1).Value = WorksheetFunction.Sum(.Range("e1:e" & LR))
End With
This piece of code replaces the last cell with new sum of everything above it: If you have 1's in E1:E5 this piece of code puts 4 in E5 no matter how many times you run the code.
 

johndrew

Board Regular
Joined
Apr 21, 2007
Messages
100
That removes my last entry in E5.

Here is what I used: Find the last inventory item used row in column (A) the go to column(E) and use that in the sum range and post one cell down.


Dim LR As Long
With Sheets("inventory")
LR = .Range("a" & Rows.Count).End(xlUp).Row
.Range("e" & LR + 1).Value = WorksheetFunction.Sum(.Range("e1:e" & LR))
End With

Thanks for your help
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,708
Messages
5,512,962
Members
408,928
Latest member
Black Vinyl

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top