Excel VBA Sum Dynamic Range All Sheets
In each sheet, I want to sum column A from A3 downward (there are no empty cells) and insert the sum in the first empty cell.
The code below works fine for sheet1, but the sum value is wrong for sheet2 onward (in sheet2 the sum of sheet1 is doubled and this same value is inserted in sheet3 onward).
I'd be grateful if you could point out what I'm doing wrong, please?
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell.Formula = WorksheetFunction.Sum(Range(Range("A3"), Range("A3").End(xlDown)))
Next ws
End Sub
excel vba
add a comment |
In each sheet, I want to sum column A from A3 downward (there are no empty cells) and insert the sum in the first empty cell.
The code below works fine for sheet1, but the sum value is wrong for sheet2 onward (in sheet2 the sum of sheet1 is doubled and this same value is inserted in sheet3 onward).
I'd be grateful if you could point out what I'm doing wrong, please?
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell.Formula = WorksheetFunction.Sum(Range(Range("A3"), Range("A3").End(xlDown)))
Next ws
End Sub
excel vba
add a comment |
In each sheet, I want to sum column A from A3 downward (there are no empty cells) and insert the sum in the first empty cell.
The code below works fine for sheet1, but the sum value is wrong for sheet2 onward (in sheet2 the sum of sheet1 is doubled and this same value is inserted in sheet3 onward).
I'd be grateful if you could point out what I'm doing wrong, please?
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell.Formula = WorksheetFunction.Sum(Range(Range("A3"), Range("A3").End(xlDown)))
Next ws
End Sub
excel vba
In each sheet, I want to sum column A from A3 downward (there are no empty cells) and insert the sum in the first empty cell.
The code below works fine for sheet1, but the sum value is wrong for sheet2 onward (in sheet2 the sum of sheet1 is doubled and this same value is inserted in sheet3 onward).
I'd be grateful if you could point out what I'm doing wrong, please?
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell.Formula = WorksheetFunction.Sum(Range(Range("A3"), Range("A3").End(xlDown)))
Next ws
End Sub
excel vba
excel vba
asked Jan 1 at 17:52
Itachi_UchihaItachi_Uchiha
144
144
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You have to identify the ranges on each worksheet as you loop through them.
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell = WorksheetFunction.Sum(ws.Range(ws.Range("A3"), ws.Range("A3").End(xlDown)))
'if you actually want a sum formula then,
'LastCell.FormulaR1C1 = "=SUM(R3C:R[-1]C)"
Next ws
End Sub
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
add a comment |
Sum Up Column From Above
'*******************************************************************************
' Purpose: In all worksheets starting from the specified first cell
' of a contiguous column range, sums up its values and returns
' the result a specified number of rows below.
'*******************************************************************************
Sub SumUpColumnFromAbove()
Const cStrRange As String = "A3" ' Cell Range Address
Const cLngOffset As Integer = 2 ' Result Offset (1 for first row below)
Dim i As Integer
With ThisWorkbook
For i = 1 To .Worksheets.Count
With .Worksheets(i).Range(cStrRange).Cells(1, 1)
' Check if range has more than one cell (row) i.e.
' the cell below is not empty.
If Not IsEmpty(.Offset(1, 0)) Then ' More than one cell.
.End(xlDown).Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Resize(.End(xlDown).Row - 1))
Else ' One cell only.
.Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Parent.Range(cStrRange))
End If
End With
Next
End With
End Sub
'*******************************************************************************
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997652%2fexcel-vba-sum-dynamic-range-all-sheets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have to identify the ranges on each worksheet as you loop through them.
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell = WorksheetFunction.Sum(ws.Range(ws.Range("A3"), ws.Range("A3").End(xlDown)))
'if you actually want a sum formula then,
'LastCell.FormulaR1C1 = "=SUM(R3C:R[-1]C)"
Next ws
End Sub
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
add a comment |
You have to identify the ranges on each worksheet as you loop through them.
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell = WorksheetFunction.Sum(ws.Range(ws.Range("A3"), ws.Range("A3").End(xlDown)))
'if you actually want a sum formula then,
'LastCell.FormulaR1C1 = "=SUM(R3C:R[-1]C)"
Next ws
End Sub
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
add a comment |
You have to identify the ranges on each worksheet as you loop through them.
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell = WorksheetFunction.Sum(ws.Range(ws.Range("A3"), ws.Range("A3").End(xlDown)))
'if you actually want a sum formula then,
'LastCell.FormulaR1C1 = "=SUM(R3C:R[-1]C)"
Next ws
End Sub
You have to identify the ranges on each worksheet as you loop through them.
Sub Sum_Dynamic_Rng()
Dim ws As Worksheet
Dim LastCell As Range
For Each ws In ThisWorkbook.Worksheets
Set LastCell = ws.Range("A3").End(xlDown).Offset(1, 0)
LastCell = WorksheetFunction.Sum(ws.Range(ws.Range("A3"), ws.Range("A3").End(xlDown)))
'if you actually want a sum formula then,
'LastCell.FormulaR1C1 = "=SUM(R3C:R[-1]C)"
Next ws
End Sub
answered Jan 1 at 17:59
user10852207user10852207
2263
2263
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
add a comment |
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
this newbie is much obliged, thumbs up
– Itachi_Uchiha
Jan 1 at 18:08
add a comment |
Sum Up Column From Above
'*******************************************************************************
' Purpose: In all worksheets starting from the specified first cell
' of a contiguous column range, sums up its values and returns
' the result a specified number of rows below.
'*******************************************************************************
Sub SumUpColumnFromAbove()
Const cStrRange As String = "A3" ' Cell Range Address
Const cLngOffset As Integer = 2 ' Result Offset (1 for first row below)
Dim i As Integer
With ThisWorkbook
For i = 1 To .Worksheets.Count
With .Worksheets(i).Range(cStrRange).Cells(1, 1)
' Check if range has more than one cell (row) i.e.
' the cell below is not empty.
If Not IsEmpty(.Offset(1, 0)) Then ' More than one cell.
.End(xlDown).Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Resize(.End(xlDown).Row - 1))
Else ' One cell only.
.Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Parent.Range(cStrRange))
End If
End With
Next
End With
End Sub
'*******************************************************************************
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
add a comment |
Sum Up Column From Above
'*******************************************************************************
' Purpose: In all worksheets starting from the specified first cell
' of a contiguous column range, sums up its values and returns
' the result a specified number of rows below.
'*******************************************************************************
Sub SumUpColumnFromAbove()
Const cStrRange As String = "A3" ' Cell Range Address
Const cLngOffset As Integer = 2 ' Result Offset (1 for first row below)
Dim i As Integer
With ThisWorkbook
For i = 1 To .Worksheets.Count
With .Worksheets(i).Range(cStrRange).Cells(1, 1)
' Check if range has more than one cell (row) i.e.
' the cell below is not empty.
If Not IsEmpty(.Offset(1, 0)) Then ' More than one cell.
.End(xlDown).Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Resize(.End(xlDown).Row - 1))
Else ' One cell only.
.Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Parent.Range(cStrRange))
End If
End With
Next
End With
End Sub
'*******************************************************************************
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
add a comment |
Sum Up Column From Above
'*******************************************************************************
' Purpose: In all worksheets starting from the specified first cell
' of a contiguous column range, sums up its values and returns
' the result a specified number of rows below.
'*******************************************************************************
Sub SumUpColumnFromAbove()
Const cStrRange As String = "A3" ' Cell Range Address
Const cLngOffset As Integer = 2 ' Result Offset (1 for first row below)
Dim i As Integer
With ThisWorkbook
For i = 1 To .Worksheets.Count
With .Worksheets(i).Range(cStrRange).Cells(1, 1)
' Check if range has more than one cell (row) i.e.
' the cell below is not empty.
If Not IsEmpty(.Offset(1, 0)) Then ' More than one cell.
.End(xlDown).Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Resize(.End(xlDown).Row - 1))
Else ' One cell only.
.Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Parent.Range(cStrRange))
End If
End With
Next
End With
End Sub
'*******************************************************************************
Sum Up Column From Above
'*******************************************************************************
' Purpose: In all worksheets starting from the specified first cell
' of a contiguous column range, sums up its values and returns
' the result a specified number of rows below.
'*******************************************************************************
Sub SumUpColumnFromAbove()
Const cStrRange As String = "A3" ' Cell Range Address
Const cLngOffset As Integer = 2 ' Result Offset (1 for first row below)
Dim i As Integer
With ThisWorkbook
For i = 1 To .Worksheets.Count
With .Worksheets(i).Range(cStrRange).Cells(1, 1)
' Check if range has more than one cell (row) i.e.
' the cell below is not empty.
If Not IsEmpty(.Offset(1, 0)) Then ' More than one cell.
.End(xlDown).Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Resize(.End(xlDown).Row - 1))
Else ' One cell only.
.Offset(cLngOffset, 0) = WorksheetFunction _
.Sum(.Parent.Range(cStrRange))
End If
End With
Next
End With
End Sub
'*******************************************************************************
edited Jan 1 at 20:16
answered Jan 1 at 19:44
VBasic2008VBasic2008
3,1192416
3,1192416
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
add a comment |
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
that works great too. You guys are masters on here! The different syntax for the same problem broadens my understanding - thank you
– Itachi_Uchiha
Jan 1 at 21:42
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
@That was the primary reason. But there are With and Resize and the 'if range only has one cell' issue i.e. the cell below is empty and finally the use of constants. Thanks for the response.
– VBasic2008
Jan 1 at 21:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997652%2fexcel-vba-sum-dynamic-range-all-sheets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown