Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default For Each sheet in WkBook problem

Sheets in workbook are A, B, C, D, E, F, G plus the two that are excluded in the code.

Total combined rows with data for all A to G sheets is about 52. (about 7 per sheet.)

If I run SheetsCopy1 I get about 5000 rows copied to Summary sheet, multiple repeats of correct data.

If I run SheetsCopy2 I see each sheet range to copy is properly selected and the Msgbox shows the correct sheet name.

The DeBug.Print lists the proper sheets A to G.

Code is in a standard module, and the downloaded example workbook looks to be German, saved as ...xlsm.

What little tid-bit am I overlooking here?

Thanks,
Howard

Sub SheetsCopy1()
Dim ws As Worksheet
Dim Rng As Range

For Each ws In ThisWorkbook.Sheets

If (ws.Name < "Summary") And (ws.Name < "Begin blad") Then

With ws

ws.Activate
Set Rng = Range("A2", Range("D2").End(xlDown))
Rng.Copy Sheets("Summary").Range("A" & Rows.Count).End(xlUp)(2)

End With

End If

Next
End Sub

Sub SheetsCopy2()
Dim ws As Worksheet
Dim Rng As Range
Dim lRow As Long

For Each ws In ThisWorkbook.Sheets

If (ws.Name < "Summary") And (ws.Name < "Begin blad") Then
'Debug.Print ws.Name
With ws
ws.Activate
Set Rng = Range("A2", Range("D2").End(xlDown))
Rng.Select
MsgBox ws.Name
End With

End If

Next
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default For Each sheet in WkBook problem

Hi Howard,
Any time I have to consolidate data from multiple sheets to a 'summary'
sheet I use the following logic:

Dim wks As Worksheet, vData, lNextRow&

With Sheets("Summary")
'Get the next row position on the summary sheet
lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

For Each wks In ThisWorkbook.Sheets
'Load the data range on each consolidation sheet into an array.
If (wks.Name < "Summary") And (wks.Name < "Begin blad") Then
vData = wks.Range("A2", wks.Range("D2").End(xlDown))

'Assign the array to the next row position
.Cells(lNextRow, 1).Resize(Ubound(vData), _
Ubound(vdata), 2) = vData

'Increment the next row position
lNextRow = lNextRow + Ubound(vData)
End If
Next 'wks
End With 'Sheets("Summary")

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default For Each sheet in WkBook problem

,
Any time I have to consolidate data from multiple sheets to a 'summary'
sheet I use the following logic:

Dim wks As Worksheet, vData, lNextRow&

With Sheets("Summary")
'Get the next row position on the summary sheet
lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

For Each wks In ThisWorkbook.Sheets
'Load the data range on each consolidation sheet into an array.
If (wks.Name < "Summary") And (wks.Name < "Begin blad") Then
vData = wks.Range("A2", wks.Range("D2").End(xlDown))

'Assign the array to the next row position
.Cells(lNextRow, 1).Resize(Ubound(vData), _
Ubound(vdata), 2) = vData

'Increment the next row position
lNextRow = lNextRow + Ubound(vData)
End If
Next 'wks
End With 'Sheets("Summary")

--
Garry


Hi Garry,

Thanks for taking a look see.

This line errors with xlUP error value -4162

lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

Tried with 15 +- rows of data already on Summary sheet and with only the headers on the sheet. Both give same error...?

Summary is a table for the A B C D columns, and the line failed, changed the table to a range, same result. Not sure that would make any difference anyway.

I also know that Excel has a personal vendetta out for me, given my luck with recent projects!

Howard


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default For Each sheet in WkBook problem

My bad! (aircode<g)

This..
lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

...should be...

lNextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default For Each sheet in WkBook problem

On Thursday, November 26, 2015 at 5:03:00 PM UTC-8, GS wrote:
My bad! (aircode<g)

This..
lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

..should be...

lNextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1

--
Garry


Hi Garry,

Thanks, actually if I had studied the code harder, I should have seen that.

I did have to make a change on this line for the Resize columns.

..Cells(lNextRow, 1).Resize(UBound(vData), 4) = vData

Works great now.

Thanks for the array approach, I like the array shot at it, but it still whacks me most often.

Howard


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,182
Default For Each sheet in WkBook problem

On Thursday, November 26, 2015 at 5:03:00 PM UTC-8, GS wrote:
My bad! (aircode<g)

This..
lNextRow = .Cells(1, .Rows.Count).End(xlUp).Row + 1

..should be...

lNextRow = .Cells(.Rows.Count, 1).End(xlUp).Row + 1

--
Garry


Hi Garry,

Thanks, actually if I had studied the code harder, I should have seen
that.

I did have to make a change on this line for the Resize columns.

.Cells(lNextRow, 1).Resize(UBound(vData), 4) = vData

Works great now.

Thanks for the array approach, I like the array shot at it, but it
still whacks me most often.

Howard


Why did you hardcode the cols? The code uses the cols in vData, which
is 4 based on your range of A:D! You should always let the code use the
array sizes for the data...

Ubound(vData), 2) = 4 since it specifies the 2nd dim (#cols)

...because the range it holds spans cols A:D. You likely forgot that
loading a worksheet range into a variant results a 2D array.

I don't even use hard values for the source range because the data
resides in a dynamic range named "InputData" on every sheet. I didn't
want to take you there with this thread in case it went over your head,
but I do this...

vData = wks.Range("InputData")

...so the code is 100% reusable as well as better self-documenting.

HTH

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update Cell in WKBook 1 From WkBook 2 pattlee Excel Discussion (Misc queries) 2 May 25th 08 01:27 PM
VBA Open Wkbook Disabling Macros Karen53 Excel Programming 9 March 21st 08 08:54 PM
How to link to a single wksheet in a wkbook that has comments RedRobyn Excel Discussion (Misc queries) 1 June 27th 06 07:40 PM
[hlp]: How to copy a sheet to new wkbook and close the var at the end pao_e_vinho[_2_] Excel Programming 2 June 27th 06 06:21 PM
Can changes to a worksheet be applied to all wksheets in wkbook? SandyM Excel Worksheet Functions 1 September 18th 05 06:21 PM


All times are GMT +1. The time now is 10:43 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"