What have i done wrong with this macro, code wont work?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,

I've tried to write a simple macro then put it in a loop, but i've never done my own loops before, I google who and followed the directions as much as posible but have not had any luck,
I think i might be close but as always I'd be here all week trying to figure out what is wrong as i haven't a CLUE?

Please can someone tell me whats done wrong or whats missing?

It would be really hand to have this as i could adapt the loop into many other jobs as well?

Here the code

Code:
Sub SubTotalAll()
Dim xrow As Long
xrow = 2
Dim lastrow As Long
lastrow = Cells(Rows.Count, 1).End(x1Up).Row


Do Until xrow = lastrow + 1
   
    
    
    Range("M1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Range("A1").Select
    Selection.FormulaR1C1 = "=SUM(R[-24]C:R[-1]C)"
    
    
    
    xrow = xrow + 1
    Loop
End Sub

Please help i think i'm close,

to summaries, the code is trying to Select M1, find blank cell input formula and do it again

Thanks

Tony
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Please Provide Some Sample Data and Details what actually you want to do ?
 
Upvote 0
Use this one instead:


Excel 2010
ALM
1AM
2JOHN200
3MARY100
4TOM300
5
6JOE600
7BILL400
8GEFF1000
9HARRY600
10JO800
11
12BOB200
13BILL500
14SUE600
15
16YORIK100
17HAMLET200
18LEAR300
19BETH700
20
21TOM200
22BILL500
Sheet9


Code:
Sub SumGroupsV3()
  Dim Ar As Range
  On Error GoTo NoData
  For Each Ar In Columns("M").SpecialCells(xlConstants).Areas
    Ar(1).Offset(Ar.Count).Formula = "=SUM(" & Ar.Address & ")"
  Next
NoData:
End Sub


Excel 2010
ALM
1AM
2JOHN200
3MARY100
4TOM300
5600
6JOE600
7BILL400
8GEFF1000
9HARRY600
10JO800
113400
12BOB200
13BILL500
14SUE600
151300
16YORIK100
17HAMLET200
18LEAR300
19BETH700
201300
21TOM200
22BILL500
Sheet9
Cell Formulas
RangeFormula
M5=SUM($M$1:$M$4)
M11=SUM($M$6:$M$10)
M15=SUM($M$12:$M$14)
M20=SUM($M$16:$M$19)
 
Upvote 0
Thank you sheetspread, that has solved my problem,
I was hoping to also find out where i went wrong on the loop code?
I realise it can be difficult to to that sometimes but it would be really hand if i could understand what i did wrong?
I'm not worried about the code in the middle (yours is much better) but just w got an error and the code didn't run?

if anyone could just guide me on this it would be something i could use a lot?

thanks

Tony
 
Upvote 0
It's Rick Rothstein's code :cool:

As for what you've written, I'm not sure you can automatically count the number of cells between spaces the same way the autosum button on the worksheet does. That's the problem with trying to extend code written by the macro recorder (note also the unneeded .select, activecell, and selection)
 
Upvote 0
Hi sheetspread,
ok thanks very much for that,
I have actually managed to get my code working, but its nowhere near as good as what you gave me so big thanks.

Tony
 
Upvote 0
It looks like this is input data. If so, I would highly recommend not adding formulas to it. Instead you can do a few things. If it's a Table then you should be using a PivotTable. If it's not a Table then you could think about using the Subtotals functionality (found on the Data tab).
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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