Hehe, yep, significantly different. OK. So the visible columns to import are F,M,N and O. We need to check EACH column and use the column with the deepest value for the LR variable.
We need to do the same thing on the import sheet, too.
Try these changes, updates marked in red:
Option Explicit Sub Consolidate() 'Author: JBeaucaire' 'Date: 2/11/2010 (2007 compatible)' 'Summary: Open all Excel files in a specific folder and merge data' ' into one master sheet (stacked)' ' Moves imported files into another folder' Dim fName As String, fPath As String, fPathDone As String, OldDir As String Dim LR As Long, NR As Long Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet, wsNew As Worksheet 'Setup Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False Set wbkNew = ThisWorkbook wbkNew.Activate Set wsNew = wbkNew.Sheets("Sheet1") wsNew.Activate 'sheet report is built into, edit as needed If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then Cells.Clear NR = 1 Else NR = Range("A" & Rows.Count).End(xlUp).Row + 1 End If 'Path and filename OldDir = CurDir 'memorizes your current working path fPath = "C:\Project1\" 'files are here fPathDone = "C:\Project1\Imported\" 'move files to here after import ChDir fPath fName = Dir("*.xls") 'filtering key, change to suit 'Import a sheet from found file Do While Len(fName) > 0 Set wbkOld = Workbooks.Open(fName) 'Open file For Each ws In ActiveWorkbook.Worksheets With wsNew .Range("A" & NR).Value = fName .Range("B" & NR) = "Worksheet: " & ws.Name .Range("A" & NR, "D" & NR).Interior.ColorIndex = 43 LR = Application.WorksheetFunction.Max( _ ws.Range("F" & Rows.Count).End(xlUp).Row, _ ws.Range("M" & Rows.Count).End(xlUp).Row, _ ws.Range("N" & Rows.Count).End(xlUp).Row, _ ws.Range("O" & Rows.Count).End(xlUp).Row) 'Find last row and copy data edit range to suit ws.Range("F2:O" & LR).SpecialCells(xlCellTypeVisible).Copy .Range("A" & NR + 1).PasteSpecial xlPasteAll NR = Application.WorksheetFunction.Max( _ .Range("A" & .Rows.Count).End(xlUp).Row, _ .Range("B" & .Rows.Count).End(xlUp).Row, _ .Range("C" & .Rows.Count).End(xlUp).Row, _ .Range("D" & .Rows.Count).End(xlUp).Row) + 1 'Next row End With Next ws wbkOld.Close False 'close file Name fPath & fName As fPathDone & fName 'move file to "imported" folder fName = Dir 'ready next filename Loop 'Cleanup ActiveSheet.Columns.AutoFit Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True ChDir OldDir 'restores your original working path End Sub
Last edited by JBeaucaire; 02-12-2010 at 07:47 AM.
_________________
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!)
And it worked!
I only had to add the dot before Range in the NR variable (.Range) and I got the result I needed.
Basically now I need to specify all columns I would like to import, but it's just a matter of copying and pasting code to suit my needs.
Now, to conclude the previous request, how can I have the filename and worksheet name on each line of columns A and B?
Something like:
File1 - Sheet1 - Text to import
File1 - Sheet1 - Text to import
File1 - Sheet2 - Text to import
File2 - Sheet1 - Text to import
File2 - Sheet1 - Text to import
This way I won't need the green line in between each imported file, I can just filter the filenames using columns A and B.
Try this:
Option Explicit Sub Consolidate() 'Author: JBeaucaire' 'Date: 2/11/2010 (2007 compatible)' 'Summary: Open all Excel files in a specific folder and merge data' ' into one master sheet (stacked)' ' Moves imported files into another folder' Dim fName As String, fPath As String, fPathDone As String, OldDir As String Dim LR As Long, NR As Long Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet, wsNew As Worksheet 'Setup Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False Set wbkNew = ThisWorkbook wbkNew.Activate Set wsNew = wbkNew.Sheets("Sheet1") wsNew.Activate 'sheet report is built into, edit as needed If MsgBox("Import new data to this report?", vbYesNo) = vbNo Then Exit Sub If MsgBox("Clear the old data first?", vbYesNo) = vbYes Then Cells.clear NR = 1 Else NR = Range("A" & Rows.Count).End(xlUp).Row + 1 End If 'Path and filename OldDir = CurDir 'memorizes your current working path fPath = "C:\Project1\" 'files are here fPathDone = "C:\Project1\Imported\" 'move files to here after import ChDir fPath fName = Dir("*.xls") 'filtering key, change to suit 'Import a sheet from found file Do While Len(fName) > 0 Set wbkOld = Workbooks.Open(fName) 'Open file For Each ws In ActiveWorkbook.Worksheets With wsNew .Range("A" & NR).Value = fName .Range("B" & NR) = "Worksheet: " & ws.Name LR = Application.WorksheetFunction.Max( _ ws.Range("F" & Rows.Count).End(xlUp).Row, _ ws.Range("M" & Rows.Count).End(xlUp).Row, _ ws.Range("N" & Rows.Count).End(xlUp).Row, _ ws.Range("O" & Rows.Count).End(xlUp).Row) 'Find last row and copy data edit range to suit ws.Range("F2:O" & LR).SpecialCells(xlCellTypeVisible).Copy .Range("C" & NR).PasteSpecial xlPasteAll LR = NR NR = Application.WorksheetFunction.Max( _ .Range("A" & .Rows.Count).End(xlUp).Row, _ .Range("B" & .Rows.Count).End(xlUp).Row, _ .Range("C" & .Rows.Count).End(xlUp).Row, _ .Range("D" & .Rows.Count).End(xlUp).Row) + 1 'Next row .Range("A" & LR, "B" & LR).AutoFill .Range("A" & LR, "B" & NR - 1) End With Next ws wbkOld.Close False 'close file Name fPath & fName As fPathDone & fName 'move file to "imported" folder fName = Dir 'ready next filename Loop 'Cleanup ActiveSheet.Columns.AutoFit Application.DisplayAlerts = True Application.EnableEvents = True Application.ScreenUpdating = True ChDir OldDir 'restores your original working path 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!)
Thanks for the quick reply.
Hmm, this way columns A and B get filled with text to import and then the filename / worksheet name is copied over.
So now A and B have filename and worksheet name, and CDEF have the text to import. But:NR = Application.WorksheetFunction.Max( _ .Range("C" & .Rows.Count).End(xlUp).Row, _ .Range("D" & .Rows.Count).End(xlUp).Row, _ .Range("E" & .Rows.Count).End(xlUp).Row, _ .Range("F" & .Rows.Count).End(xlUp).Row) + 1 'Next row .Range("A" & LR, "B" & LR).AutoFill .Range("A" & LR, "B" & NR - 1)
1. the worksheet name increases on every row (filename is shown correctly);
2. the text gets imported only from row #2, so the last row in Sheet1 already appears as if it was from Sheet2.
Try this:
.Range("A" & LR, "B" & LR).Copy .Range("A" & LR + 1, "B" & NR - 1)
_________________
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!)
Hi,
It worked perfectly, now I have exactly the result I want.
You're a genius, thanks again a lot for your kind availability!![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks