Macro Help: Copy Data from a Range of Sheets to a Master Sheet
Solved/Closed
supermate
Posts
2
Registration date
Thursday January 17, 2013
Status
Member
Last seen
January 17, 2013
-
Jan 17, 2013 at 12:55 AM
supermate - Jan 23, 2013 at 12:16 AM
supermate - Jan 23, 2013 at 12:16 AM
Related:
- Macro Help: Copy Data from a Range of Sheets to a Master Sheet
- Spell number in excel without macro - Guide
- Macro excel download - Download - Spreadsheets
- Excel macro to create new sheet based on value in cells - Guide
- Run macro on opening workbook - Guide
- Excel vba assign macro to button programmatically - Guide
7 responses
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
Jan 17, 2013 at 10:40 AM
Jan 17, 2013 at 10:40 AM
Hi Supermate,
Just a few questions:
1. How many sheets are there? Are "All Deadlines" and "Template" the only sheets excluded? And how many sheets are there between "All Deadlines" and "Template"? and preferably there names.
This because I don't know how to loop through sheets between to specific sheets.
2. No questions.
3. Are rows 1 and 2 filled with data?
4. No questions.
Best regards,
Trowa
Just a few questions:
1. How many sheets are there? Are "All Deadlines" and "Template" the only sheets excluded? And how many sheets are there between "All Deadlines" and "Template"? and preferably there names.
This because I don't know how to loop through sheets between to specific sheets.
2. No questions.
3. Are rows 1 and 2 filled with data?
4. No questions.
Best regards,
Trowa
supermate
Posts
2
Registration date
Thursday January 17, 2013
Status
Member
Last seen
January 17, 2013
1
Jan 17, 2013 at 05:11 PM
Jan 17, 2013 at 05:11 PM
Hi Trowa and thanks for your reply. Here are the answers to your questions:
1. The number of sheets will vary, increasing over time. I have another macro to create a new sheet from the Template worksheet every time I get a new project. At the moment there are three sheets at the start titled "Create New Project" "Project Dashboard" and "All Deadlines". following all deadlines will come individual worksheets dedicated to each project and the worksheet "Template" is then located right at the end. The sheets between "All Deadlines" and "Template" are currently just a bunch of random project names I came up with to use while I'm building the workbook.
3. Rows 1 and 2 have column headers in them.
I hope this is helpful?
Matt
1. The number of sheets will vary, increasing over time. I have another macro to create a new sheet from the Template worksheet every time I get a new project. At the moment there are three sheets at the start titled "Create New Project" "Project Dashboard" and "All Deadlines". following all deadlines will come individual worksheets dedicated to each project and the worksheet "Template" is then located right at the end. The sheets between "All Deadlines" and "Template" are currently just a bunch of random project names I came up with to use while I'm building the workbook.
3. Rows 1 and 2 have column headers in them.
I hope this is helpful?
Matt
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
Jan 21, 2013 at 12:09 PM
Jan 21, 2013 at 12:09 PM
Hi Matt,
Ok, make a copy of your file, paste the code and try it out. Let me know if it works ok or if you want some adjustments.
Best regards,
Trowa
Ok, make a copy of your file, paste the code and try it out. Let me know if it works ok or if you want some adjustments.
Sub MoveData() Dim ws As Worksheet Dim lRow, dRow As Integer Sheets("All Deadlines").Rows("3:" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents For Each ws In Sheets If ws.Name = "Create New Project" _ Or ws.Name = "Project Dashboard" _ Or ws.Name = "All Deadlines" _ Or ws.Name = "Template" Then GoTo Nextws dRow = Sheets("All Deadlines").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row lRow = Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Row Sheets(ws.Name).Rows("14:" & lRow).Copy Sheets("All Deadlines").Range("A" & dRow) Nextws: Next ws End Sub
Best regards,
Trowa
Didn't find the answer you are looking for?
Ask a question
TrowaD
Posts
2921
Registration date
Sunday September 12, 2010
Status
Moderator
Last seen
December 27, 2022
555
Jan 22, 2013 at 11:20 AM
Jan 22, 2013 at 11:20 AM
Great testing Matt, you are right.
This line is the problem:
The line clears data from row 3 until the last row used. When there is no data the last row used is row 2. So Rows 2 and 3 will be cleared.
So first we need to determine if any rows need to be cleared. We do this by checking if there is any data in cell A3.
Replace the above line by:
Or just copy the entire code:
Best regards,
Trowa
This line is the problem:
Sheets("All Deadlines").Rows("3:" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents
The line clears data from row 3 until the last row used. When there is no data the last row used is row 2. So Rows 2 and 3 will be cleared.
So first we need to determine if any rows need to be cleared. We do this by checking if there is any data in cell A3.
Replace the above line by:
If Range("A3") <> vbNullString Then Sheets("All Deadlines").Rows("3:" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents End If
Or just copy the entire code:
Sub MoveData() Dim ws As Worksheet Dim lRow, dRow As Integer If Range("A3") <> vbNullString Then Sheets("All Deadlines").Rows("3:" & Range("A" & Rows.Count).End(xlUp).Row).ClearContents End If For Each ws In Sheets If ws.Name = "Create New Project" _ Or ws.Name = "Project Dashboard" _ Or ws.Name = "All Deadlines" _ Or ws.Name = "Template" Then GoTo Nextws dRow = Sheets("All Deadlines").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row lRow = Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Row Sheets(ws.Name).Rows("14:" & lRow).Copy Sheets("All Deadlines").Range("A" & dRow) Nextws: Next ws End Sub
Best regards,
Trowa
Just one more question: when I run the macro repeatedly it doesn't always behave the same. Sometimes it duplicates one of the copied rows and sometimes it overwrites one of the header rows. Any ideas why that might be?
edit: my last couple of runs have resulted in the number of rows of data increasing by 1 row per run of the macro and the extra row is a duplicate of one of the existing rows. Can you see anything in the macro that might be causing this?
edit: my last couple of runs have resulted in the number of rows of data increasing by 1 row per run of the macro and the extra row is a duplicate of one of the existing rows. Can you see anything in the macro that might be causing this?