Solved

Dynamic autosum across macro

Posted on 2010-11-15
11
788 Views
Last Modified: 2012-06-21
I want to be able to use autosum across, in a macro. the amount of rows will always be different.

I was able to use the following macro, to dynamically autosum a column:


    For Each NumRange In Columns("I").SpecialCells(xlConstants, xlNumbers).Areas
        SumAddr = NumRange.Address(False, False)
        NumRange.Offset(NumRange.Count, 0).Resize(1, 6).Formula = "=SUM(" & SumAddr & ")"
        c = NumRange.Count
    Next NumRange


I would like to do the same in rows.

I've attached a PDF of the report I'm working with.

Please help

 Example.pdf

0
Comment
Question by:PLA_LTM
  • 5
  • 5
11 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34138362
Why doesn't this work? (Can't view the report by the way).

For Each NumRange In Rows(5).SpecialCells(xlConstants, xlNumbers).Areas
        SumAddr = NumRange.Address(False, False)
        NumRange.Offset(0, NumRange.Count).Resize(6, 1).Formula = "=SUM(" & SumAddr & ")"
        c = NumRange.Count
    Next NumRange

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34138761
Of course the 5 should be the right row number.
0
 

Author Comment

by:PLA_LTM
ID: 34139177

hmm this seems to work but if i have a varying number of rows, what do i do?

sometimes i may have 4 rows others can be in 10 or greater.
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 34139271
Just add more rows...

Range("5:10")

HTH
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34139476
That might attach areas. Try
For i = 5 to 10

For Each NumRange In Rows(i).SpecialCells(xlConstants, xlNumbers).Areas

        SumAddr = NumRange.Address(False, False)

        NumRange.Offset(0, NumRange.Count).Resize(6, 1).Formula = "=SUM(" & SumAddr & ")"

        c = NumRange.Count

    Next NumRange

Next

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:PLA_LTM
ID: 34164859
Thank you TommySzalapski for you code.  I'm not sure if i'm implementing it correctly.

I can get it to Sum across but not for all my records.  The "6" in "Resize(6, 1)"  seems to limit only the summation of 6 records.  Is there a variable i can use here to sum exactly the number of records?  (whether it's 1 or 1000 records)  (is this even possible?)

I've added an image of my table, in hopes to illustrate my issue better.

The total across will always appear in Column G.  

The number of records can range any where from 1 to several thousand.

 Example of my records
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34165307
Ah. Now that I can see your data. Use this.
Dim i As Integer, lastrow As Integer

lastrow = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

For Each NumRange In Rows(i).SpecialCells(xlConstants, xlNumbers).Areas
        SumAddr = NumRange.Address(False, False)
        NumRange.Offset(0, NumRange.Columns.Count - NumRange.Column + 2).Formula = "=SUM(" & SumAddr & ")"
        c = NumRange.Count
    Next NumRange
Next

Open in new window

0
 

Author Comment

by:PLA_LTM
ID: 34165485
I feel like we're on the right track.

That seems to work, Column G seems to have the right value.  However, it seems to overwrite columns H-K with a formula as well.
I've added screen caps of what's happening with the macro and what should be happening.

 Result almost there What result should be
I greatly appreciate you help with this.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 34165677
I forgot to resize the range.
Also there's no need to loop within the same row.
Dim i As Integer, lastrow As Integer



lastrow = Range("A" & Rows.Count).End(xlUp).Row



For i = 2 To lastrow



        Set Numrange = Rows(i).SpecialCells(xlConstants, xlNumbers).Areas(1)

        SumAddr = Numrange.Address(False, False)

        Numrange.Offset(0, Numrange.Columns.Count - Numrange.Column + 2).Resize(1, 1).Formula = "=SUM(" & SumAddr & ")"



Next

Open in new window

0
 

Author Closing Comment

by:PLA_LTM
ID: 34165707
OMG!!! That worked perfectly!!!

Thank you,
0
 

Author Comment

by:PLA_LTM
ID: 34166488

TommySzalapski, thank you for your help.

I have another, VBS question posted.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26624318.html

would you mind checking to see if you can assist me here as well?
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now