Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have the following code which acts on one sheet. How could I get it to
loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub sumrows()
Dim LastRow As Long For Each sht In Sheets With sht .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, _ CopyToRange:=.Range("X1"), _ CriteriaRange:="", _ Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit For cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With Next sht End Sub "Jeff Kelly" wrote: I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub sumrows()
Dim LastRow As Long Dim sh As Worksheet For Each sh In ThisWorkbook.Worksheets With sh .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With Next End Sub regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Jeff Kelly" wrote: I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
See if this way works for you...
Sub SumRows() Dim LastRow As Long Dim Cell As Range Dim WS As Worksheet For Each WS In Worksheets With WS .Range("B:B").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each Cell In .Range("X2:X" & LastRow) If Cell.Value = "" Then Exit Sub Cell.Offset(0, 1).Value = _ Application.WorksheetFunction.SumIf( _ .Range("B:B"), "=" & Cell.Value, .Range("D:D")) Next End With Next End Sub -- Rick (MVP - Excel) "Jeff Kelly" wrote in message ... I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
See if this way works for you...
Sub SumRows() Dim LastRow As Long Dim Cell As Range Dim WS As Worksheet For Each WS In Worksheets With WS .Range("B:B").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each Cell In .Range("X2:X" & LastRow) If Cell.Value = "" Then Exit Sub Cell.Offset(0, 1).Value = _ Application.WorksheetFunction.SumIf( _ .Range("B:B"), "=" & Cell.Value, .Range("D:D")) Next End With Next End Sub -- Rick (MVP - Excel) "Jeff Kelly" wrote in message ... I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
r: you don't want to exit the sub, you want to change the code to exit the
for loop. "r" wrote: Sub sumrows() Dim LastRow As Long Dim sh As Worksheet For Each sh In ThisWorkbook.Worksheets With sh .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With Next End Sub regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Jeff Kelly" wrote: I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Joel makes a good argument in response to r's posting which applies to my
response as well. Change this line... If Cell.Value = "" Then Exit Sub to this... If Cell.Value = "" Then Exit For -- Rick (MVP - Excel) "Rick Rothstein" wrote in message ... See if this way works for you... Sub SumRows() Dim LastRow As Long Dim Cell As Range Dim WS As Worksheet For Each WS In Worksheets With WS .Range("B:B").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each Cell In .Range("X2:X" & LastRow) If Cell.Value = "" Then Exit Sub Cell.Offset(0, 1).Value = _ Application.WorksheetFunction.SumIf( _ .Range("B:B"), "=" & Cell.Value, .Range("D:D")) Next End With Next End Sub -- Rick (MVP - Excel) "Jeff Kelly" wrote in message ... I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
you are right ... I have not read carefully
regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Joel" wrote: r: you don't want to exit the sub, you want to change the code to exit the for loop. "r" wrote: Sub sumrows() Dim LastRow As Long Dim sh As Worksheet For Each sh In ThisWorkbook.Worksheets With sh .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With Next End Sub regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Jeff Kelly" wrote: I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
you are right ... I have not read carefully
regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Joel" wrote: r: you don't want to exit the sub, you want to change the code to exit the for loop. "r" wrote: Sub sumrows() Dim LastRow As Long Dim sh As Worksheet For Each sh In ThisWorkbook.Worksheets With sh .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With Next End Sub regards r Il mio ultimo lavoro ... http://excelvba.altervista.org/blog/...ternative.html "Jeff Kelly" wrote: I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Many thank to all.
Why did I not think of that ? ++++++++ "Jeff Kelly" wrote in message ... I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"r" wrote:
you are right ... I have not read carefully and I was in good company :-) regards r |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Having now got totals on each sheet, how do I summate each item on a summary
sheet. Go easy on me as at 72 years of age I'm only now just learning VBA +++++++++ "Jeff Kelly" wrote in message ... I have the following code which acts on one sheet. How could I get it to loop through all sheets. Sub sumrows() Dim LastRow As Long With Sheets("Jan 2009") .Range("b:b").AdvancedFilter _ Action:=xlFilterCopy, CopyToRange:=.Range("X1"), _ CriteriaRange:="", Unique:=True LastRow = .Cells(.Rows.Count, "X").End(xlUp).Row For Each cell In .Range("X2:X" & LastRow) If cell.Value = "" Then Exit Sub cell.Offset(0, 1) = _ Application.WorksheetFunction.SumIf( _ .Range("b:b"), "=" & cell.Value, .Range("D:D")) Next End With End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Loop through worksheets | Excel Programming | |||
Naming Worksheets - Loop within a loop issue | Excel Programming | |||
Naming Worksheets - Loop within a loop issue | Excel Programming | |||
(Complex) Loop within loop to create worksheets | Excel Programming | |||
Loop worksheets | Excel Programming |