Insert a code for auto sum

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
143
I found the first code below, created by mirabeau, and it works great. It's basically the code to do the same as Autosum. But what I need to do is insert this into my code that I'm working on into a specific cell. In my code, the line that reads:

Range("s" & R.Row).Value = 0

I need the value to be the result of the autosum code.

If anyone has any ideas, It would be much appreciated. Thanks.

mirabeau's auto sum code:
Code:
Sub replicate_alt_equal()
Dim a As Range, b, tr As Range
With ActiveCell
On Error Resume Next
Set a = Cells(1, .Column).Resize(.Row - 1)
b = a
a.SpecialCells(2, 2) = ""
If .End(3).Offset(-1) = "" Then
    Set tr = .End(3)
Else
    Set tr = .End(3).End(3)
End If
.Formula = _
    "=Sum(" & .End(3).Address(0, 0) & ":" & tr.Address(0, 0) & ")"
Range(.Offset(-1), tr).Copy
a.Value = b
End With
End Sub

my code:
Code:
Sub inset2rows()
'
' inset2rows Macro
'

'
    Range(ActiveCell.Row & ":" & ActiveCell.Row).Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveCell.Offset(-1, 0).Select
    Selection.Resize(1, 13).Select
    Set Rng = Selection
    Selection.Copy
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste
    Selection.Resize(1, 19).Select
    Set Rng = Selection
    For Each R In Rng
    Range("s" & R.Row).Value = 0
    Application.CutCopyMode = False
    
Next R
    
    
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Don't need all that code for autosum, with the cell selected, all you need is

Code:
Application.Sendkeys ("&=")

But your other code appears to have some flaws, could you explain what you're trying to do.

I can see that you waht to insert 2 rows below the active cell, then copy the row above into the first inserted row (columns A:M).

But then what? Whay are you expanding the range to Column S?
What is the purpose of
Code:
For Each R in Range

With the rest of your code that is only going to perform the same action 19 times on the same cell!

Where should the Autosum be placed, and what should it be summing in relation to the selection?
 
Upvote 0
Your absolutly correct, there is no need for that part of the code. After I posted my question I ran accross my problem, and I'm on my way to figuring it out. Thanks for your time.:)
 
Upvote 0
Not sure if I've guessed your intentions correctly,

Code:
Sub Insert_2_Rows()
With Selection
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    .EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
End With
With ActiveSheet
     .Cells(ActiveCell.Row - 1, 1).Resize(, 13).Copy .Cells(ActiveCell.Row, 1)
     .Cells(ActiveCell.Row, 19).FormulaR1C1 = "=SUM(RC1:RC13)"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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