Insert a code for auto sum

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
138
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
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
10,873
Office Version
  1. 2019
Platform
  1. Windows
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?
 

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
138
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.:)
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
10,873
Office Version
  1. 2019
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,368
Messages
5,528,276
Members
409,814
Latest member
Leon_Al

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top