Hi all,
The following problem exists...
I have an Excel2003 file which contains various sheets. They are named like "aa Pegatron", "bb Pegatron" etc. These sheets can contain any number of lines with data (up to max 5000 lines). The sheets have all the same format / layout.
I now need a macro to pull the data of all non-empty lines in columns D, E, F, H and J from all these various sheets into 1 accumulated sheet named "Overall sheet".
What code do I need to use so that the macro would recognize the different sheetnames (whereby Pegatron is the fixed value but the aa, bb, cc etc in front of it will change in the sheetname) and whereby all lines in these sheets are copied into the overall sheet without overwriting any existing lines in that sheet.
Attached an example file for your reference.
Any help would be highly appreciated!!
Peter
Last edited by Bax; 10-25-2010 at 10:09 AM.
In your actual sheet:
1) open the VBEditor (Alt-F11)
2) Insert an empty module (Insert > Module)
3) Paste in this macro code
Option Explicit Sub CollectData() Dim ws As Worksheet Dim wsDest As Worksheet: Set wsDest = ThisWorkbook.Sheets("Overall sheet") Dim LR As Long Application.ScreenUpdating = False wsDest.Range("A2:A" & Rows.Count).EntireRow.Clear For Each ws In ThisWorkbook.Worksheets If InStr(ws.Name, "Pegatron") > 0 Then LR = ws.Range("A" & Rows.Count).End(xlUp).Row ws.Range("D2:F" & LR & ",H2:H" & LR & ",J2:J" & LR).Copy wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats End If Next ws Application.ScreenUpdating = True End Sub
4) Open the ThisWorkbook module and paste in this code:
Private Sub Workbook_Open() Call CollectData End Sub
5) Open the Overall Sheet module and paste in this code:
Private Sub Worksheet_Activate() Call CollectData End Sub
6) Close the editor and save your workbook
Now every time you open the workbook or bring up the Overall Sheet onscreen the data will refresh from every other sheet with the word "Pegatron" in the sheet name.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
excel 2007
i have an issue which i would much appreciate your assistance on:
for those of you who use excel data sheets, you woud know that you use small cirles, either filled in black or left white, to show preferences, etc. i am experiencing that when i have to do a copy and paste, to move the copy of one circle to another location, i click on the first one, the original--this circle already moves about five rows away, and then when i paste ok at the proposed location, again it gets pasted far away from where it was targeted for. it is a little frustrating!
thank you
Hi JBeaucaire,
Many thanks for the fast reply!
Works fine, I only didn't paste the code in the "ThisWorkbook" as the file seems to go into an indefinite loop... without this code it works fine as soon as I open the "Overall sheet"!
If you don't mind spending a few more minutes on this, I have an additional question...
On the various "Pegatron" sheets, there are e.g. all max 5000 lines prefilled with VLOOKUP formulas etc. So I have row 2 up to 5000 filled with "#N/A" data in the colums D, E, F, H and J until this gets filled.
The code that you provided is pulling all these "#N/A" data in the "Overall sheet" as well. Is there anything that I (or you to be more exact... ) could add in the code that would overlook the "#N/A" lines in the various sheets?
And again, many thanks for your help here... is saving us lots of time...
Peter
Try this, it will examine the imported data for errors in column A and delete those rows.
Option Explicit Sub CollectData() Dim ws As Worksheet Dim wsDest As Worksheet: Set wsDest = ThisWorkbook.Sheets("Overall sheet") Dim LR As Long Application.ScreenUpdating = False wsDest.Range("A2:A" & Rows.Count).EntireRow.Clear For Each ws In ThisWorkbook.Worksheets If InStr(ws.Name, "Pegatron") > 0 Then LR = ws.Range("A" & Rows.Count).End(xlUp).Row ws.Range("D2:F" & LR & ",H2:H" & LR & ",J2:J" & LR).Copy wsDest.Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValuesAndNumberFormats End If Next ws Range("A2:A" & Rows.Count).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete xlShiftUp Range("A2").Select If Range("A2") = "" Then Range("A2") = "no data" Application.ScreenUpdating = True End Sub
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Terrific!! Works perfectly!
Many many thanks!
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks