Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel AutoSum

Posted on 2013-01-16
9
Medium Priority
?
516 Views
Last Modified: 2013-01-16
Hello,
I have a workBook with multiple sheets (varies) ,
I'm trying to Auto Sum Columns (M,N,P,Q), Starting Raw M2,N2,P2,Q2.
And the summary be at the end of the columns (M,N,P,Q).

Do the same on all sheets.
The number of Raws varies.

I wonder if there is a vb code that can do this.

Any help is appreciated.
0
Comment
Question by:W.E.B
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 26

Accepted Solution

by:
redmondb earned 1000 total points
ID: 38785506
Hi, Wass_QA.

Please see attached. If the code finds that the last used cell in column M is simply the total for that column then it assumes that this sheet has already be done and so it skips it. Sheet1 is like this.

The code is...
Option Explicit

Sub Auto_Total()
Dim xSheet    As Worksheet
Dim xLast_row As Long
Dim xCol      As Variant

For Each xSheet In ThisWorkbook.Sheets
    If xSheet.UsedRange.Rows.Count < 0 Then Debug.Print "?!"
    xLast_row = xSheet.[A1].SpecialCells(xlLastCell).Row
    If xSheet.Range("M" & xLast_row).Formula = "=SUM(M2:M" & xLast_row - 1 & ")" Then
        MsgBox ("Total found for """ & xSheet.Name & """ - sheet bypassed.")
    Else
        For Each xCol In Array("M", "N", "P", "Q")
            xSheet.Range(xCol & xLast_row + 1).Formula = Replace("=SUM(|2:|" & xLast_row & ")", "|", xCol)
        Next
    End If
Next

End Sub

Open in new window

Regards,
Brian.Auto-Total.xls
0
 

Author Comment

by:W.E.B
ID: 38785534
Hello redmondb,
Thanks again for your help,

The code is working well on all sheets except on sheet 1

thanks,
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38785544
Wass_QA,

The code is working well on all sheets except on sheet 1
Nope - please see my first post! Would you like something different?

Thanks,
Brian.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:W.E.B
ID: 38785559
Hello,
it is not giving me any totals on sheet1,
All other sheets Total is working great.

Thanks,
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38785574
Wass_QA,

This was explained in my first post! What do you want me to do when the macro finds that there are already totals on the last line? If I simply create a new set of totals then they'll be completely wrong - giving double the correct answer!

Instead of doing something that I know is wrong, I bypass the sheet. Again, what would you prefer me to do?

(BTW, the version above copes badly with a sheet with no data. I have a new version to handle this but I'll wait for your answer to this post before sending it.)

Thanks,
Brian.
0
 

Author Comment

by:W.E.B
ID: 38785589
ok, sorry,
I figured it out,
The Total was coming up way down the column.
I had some characters I didnt delete.

thank you very much.
0
 

Author Closing Comment

by:W.E.B
ID: 38785594
Thank you very much.
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38785604
Wass_QA,

The attached handles sheets with no data. The code is...
Option Explicit

Sub Auto_Total()
Dim xSheet    As Worksheet
Dim xLast_row As Long
Dim xCol      As Variant

For Each xSheet In ThisWorkbook.Sheets
    If xSheet.UsedRange.Rows.Count < 0 Then Debug.Print "?!"
    xLast_row = xSheet.[A1].SpecialCells(xlLastCell).Row
    If xLast_row < 2 Then
        MsgBox ("No data in """ & xSheet.Name & """ - sheet bypassed.")
    Else
        If xSheet.Range("M" & xLast_row).Formula = "=SUM(M2:M" & xLast_row - 1 & ")" Then
            MsgBox ("Total found for """ & xSheet.Name & """ - sheet bypassed.")
        Else
            For Each xCol In Array("M", "N", "P", "Q")
                xSheet.Range(xCol & xLast_row + 1).Formula = Replace("=SUM(|2:|" & xLast_row & ")", "|", xCol)
            Next
        End If
    End If
Next

End Sub

Open in new window

Regards,
Brian.Auto-Total-V2.xls
0
 
LVL 26

Expert Comment

by:redmondb
ID: 38785623
Thanks, Wass_QA, but please be sure to use the version in my last post!

As I may have mentioned to you before, I keep an eye on "my" closed questions for at least a couple of weeks, so if you have any further issues with this code please feel free to post here.

Regards,
Brian.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question