Hi everyone,
I've got several Excel files that need to be edited separately.
Once they have been edited, I need to paste the content of each file in a single separate Excel file.
Is there a way to retrieve the content from all the files in a folder, and merge it in a single file?
For example:
3 separate files > File1, File2, File3
Merged file, sheet1
File1
File2
File3
I'd really appreciate any help.
Last edited by paokun; 02-06-2010 at 10:52 AM.
Hi paokun
A good place to start is with workbook by ravishankar
http://www.excelforum.com/attachment...eet-nimrod.xls
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hi pike,
Wow, this is a great macro!
Thanks, it does the job pretty neatly.
Now, starting from this sheet, how can I import only the visible cells?
If that's not possible, being able to import only specific columns would already help.
Last edited by paokun; 02-03-2010 at 06:36 PM.
hey paokun
try something like this as its easier to follow and change
Sub ptest() Dim wbIn As Worksheet, shtOutOne As Worksheet, shtOutTwo As Worksheet Set wbIn = ThisWorkbook.Worksheets(2) Set shtOutOne = Workbooks.Open("C:\Documents and Settings\User\My Documents\New Folder\ptest1.xls").Worksheets(3) Set shtOutTwo = Workbooks.Open("C:\Documents and Settings\User\My Documents\New Folder\ptest2.xls").Worksheets(3) shtOutOne.Range("A1").CurrentRegion.Copy Destination:=wbIn.Range("A1") shtOutTwo.Range("A1").CurrentRegion.Copy Destination:=wbIn.Range("A" & wbIn.Cells(Rows.Count, 1).End(xlUp).Row + 1) Set wbIn = Nothing Set shtOutOne = Nothing Set shtOutTwo = Nothing End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Hi pike,
Thanks for your new reply and sorry for my old one.
Where should I put that code?
Could you please attach a spreadsheet containing it?
Thanks :-)
Hi paokun
have a look at the code in this one
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
Pike, Paokun,
This variation on Pike's macro allows you to expand/contract the # of files in question by only editing one line of code...marked in red. Just add more filenames.
Option Explicit Sub ptest() Dim wbIn As Worksheet, i As Long, MyFiles Set wbIn = ThisWorkbook.Worksheets(1) MyFiles = Array("File1.xls", "File2.xls") For i = LBound(MyFiles) To UBound(MyFiles) Workbooks.Open (ThisWorkbook.Path & "/" & MyFiles(i)) Worksheets(1).Range("A1").CurrentRegion.Copy Destination:=wbIn.Range("A" & Rows.Count).End(xlUp).Offset(1, 0) ActiveWorkbook.Close False Next Set wbIn = Nothing End Sub
How/Where to install the macro:
1. Open up your workbook
2. Get into VB Editor (Press Alt+F11)
3. Insert a new module (Insert > Module)
4. Copy and Paste in your code (given above)
5. Get out of VBA (Press Alt+Q)
6. Save your sheet
The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
Last edited by JBeaucaire; 02-04-2010 at 06:06 PM.
_________________
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 pike,
Thanks, getting closer. Actually this extracts the first horizontal line of a spreadsheet, but how can I make it extract a vertical column (or a range of cells?).
I have the advantage that the text is in the same column in all files.
Actually I'm dealing with hundreds of files so it would be also better to make it extract the content of all files in a folder, rather than having to specify each file name. Also, the files get open when running the macro, isn't there a way to keep them close?
And one more thing, is there a way to extract only the visible cells? In this case, rather than selecting a column or a range of cells, extracting only the visible cells in each file would be perfect.
This is pure science to me, so I have no clue of how you came up with this code.
Hi JBeaucaire,
Thanks for your post!
This is an improvement because now the files get closed when the macro stops running.
Also, I had to remove & "]" from the line below as it couldn't find the files.
Workbooks.Open (ThisWorkbook.Path & "]" & MyFiles(i))
So now I get the row 1 of each file imported, but as I wrote above, is there a way to import a range of cells? Or only the visible cells?
Sorry, that was supposed to be an "/" not a "]".
If you want to:
1) Open all the files in a single folder and import them into a single sheet, data stacked
2) Close the imported files
I have a standard macro for that, you won't have to list the files, just set the correct path.
What's the range of data or column(s) you want to import?
What's the path to the folder?
Are all the files named similarly? Or should we just import everything in that folder regardless of 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!)
Here's the standard macro. I've highlighted the sections you would need to edit to suit your needs.
Option Explicit Sub Consolidate() 'Author: JBeaucaire' 'Date: 9/15/2009 (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 'Setup Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False Set wbkNew = ThisWorkbook wbkNew.Activate Sheets("Master").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:\My Documents\ExcelFiles" 'files are here fPathDone = "C:\My Documents\ExcelFiles\Imported" 'move files to here after import ChDir fPath fName = Dir("*.xl*") 'filtering key, change to suit 'Import a sheet from found file Do While Len(fName) > 0 Set wbkOld = Workbooks.Open(fName) 'Open file LR = Range("F" & Rows.Count).End(xlUp).Row 'Find last row and copy data edit range to suit Range("A1:A" & LR).EntireRow.Copy _ wbkNew.Sheets("Master").Range("A" & NR) wbkOld.Close True 'close file NR = Range("A" & Rows.Count).End(xlUp).Row + 1 'Next row 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-10-2010 at 08:48 PM.
_________________
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, thanks for the quick reply.
1) Open all the files in a single folder and import them into a single sheet, data stacked
Yes, basically I just put the macro file in the folder and would like to import all the files in this folder. If the macro file needs to be put in a separate folder, then I'd like to import the files that are in: C:\Project1\
2) Close the imported files
Yes, that would be better.
3) I'd like to import columns F M N O and exclude the header line 2 in each file.
4) I'd like to have the filename and worksheet appear in the export file so that I know where the text comes from.
The four columns above are the only visible columns, all the rest is hidden. Still, probably it's more complicated to have the macro import only the visible text, is that why you asked for the range of cells?
Thanks again for your help, I really appreciate it.
Ops sorry I was replying to your message then I saw the macro.
Can you edit the macro based on the info I wrote above?
_________________
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!)
Hmm, just read your answers, always more features needed than originally thought...
You can edit that import path, yes?
I would put the macro file in a separate folder...
Already does that...2) Close the imported files
Yes, that would be better.
Um...FMNO should land in columns ABCD?3) I'd like to import columns F M N O and exclude the header line 2 in each file.
Rows3---> bottom of the data? That part is already highlighted, you can edit that...
Listed where? Think of everything I might as next in regard to this item and include a complete explanation of the output requirements. (nudge)4) I'd like to have the filename and worksheet appear in the export file so that I know where the text comes from.
Nah, it's equally cumbersome or equally simple...depends on your comfort level.The four columns above are the only visible columns, all the rest is hidden. Still, probably it's more complicated to have the macro import only the visible text, is that why you asked for the range of cells?
Copying all the visible data only, that could be helpful, yes indeed.
Last edited by JBeaucaire; 02-04-2010 at 06:51 PM.
_________________
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!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks