Importing data from numerous closed workbooks

SGlobal

New Member
Joined
May 10, 2011
Messages
7
Hello,

This is my first time posting so I apologize if its not in the correct sub forum. I am new to excel VBA...very new. Just basic stuff however I am working on improving some of our day to day activities and was wondering if this could be done with excel VBA.

What I am trying to do is open up a workbook, click on a macro button and that macro routes to a folder looks in to each workbook within that folder and pulls in all the data on to the active workbook.

Is this possible? I know I can transfer data from all open workbooks to active work book but can I import from closed workbooks? Also the names of the closed workbooks change everyday. So basically this macro would have to just look in the folder and however many excel files are in that folder, it needs to open them, copy the data and import. If there is no way around the constant name changes ergo the files must have a certain name for mapping reasons then we can work around that over here. I can have that department just name the file something specific every time they download it.

Please let me know if this is possible or if theres a workaround for this.

Thanks,
Saad
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Sub Combine()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim J As Integer<o:p></o:p>
<o:p> </o:p>
On Error Resume Next<o:p></o:p>
Sheets(1).Select<o:p></o:p>
Worksheets.Add ' add a sheet in first place<o:p></o:p>
Sheets(1).Name = "Combined"<o:p></o:p>
<o:p> </o:p>
' copy headings<o:p></o:p>
Sheets(2).Activate<o:p></o:p>
Range("A1").EntireRow.Select<o:p></o:p>
Selection.Copy Destination:=Sheets(1).Range("A1")<o:p></o:p>
<o:p> </o:p>
' work through sheets<o:p></o:p>
For J = 2 To Sheets.Count ' from sheet 2 to last sheet<o:p></o:p>
Sheets(J).Activate ' make the sheet active<o:p></o:p>
Range("A1").Select<o:p></o:p>
Selection.CurrentRegion.Select ' select all cells in this sheets<o:p></o:p>
<o:p> </o:p>
' select all lines except title<o:p></o:p>
Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1).Select<o:p></o:p>
<o:p> </o:p>
' copy cells selected in the new sheet on last line<o:p></o:p>
Selection.Copy Destination:=Sheets(1).Range("A65536").End(xlUp)(2)<o:p></o:p>
Next<o:p></o:p>
End Sub<o:p></o:p>
 
Upvote 0
The first one was to combine worksheets in one spreadsheet to one sheet and this one is to combine workbooks onto seperate sheets in one workbook. Hope that's what you were asking for.

Sub CombineWorkbooks()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Dim FilesToOpen<o:p></o:p>
Dim x As Integer<o:p></o:p>
<o:p> </o:p>
On Error GoTo ErrHandler<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
<o:p> </o:p>
FilesToOpen = Application.GetOpenFilename _<o:p></o:p>
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", _<o:p></o:p>
MultiSelect:=True, Title:="Files to Merge")<o:p></o:p>
<o:p> </o:p>
If TypeName(FilesToOpen) = "Boolean" Then<o:p></o:p>
MsgBox "No Files were selected"<o:p></o:p>
GoTo ExitHandler<o:p></o:p>
End If<o:p></o:p>
<o:p> </o:p>
x = 1<o:p></o:p>
While x <= UBound(FilesToOpen)<o:p></o:p>
Workbooks.Open FileName:=FilesToOpen(x)<o:p></o:p>
Sheets().Move After:=ThisWorkbook.Sheets _<o:p></o:p>
(ThisWorkbook.Sheets.Count)<o:p></o:p>
x = x + 1<o:p></o:p>
Wend<o:p></o:p>
<o:p> </o:p>
ExitHandler:<o:p></o:p>
Application.ScreenUpdating = True<o:p></o:p>
Exit Sub<o:p></o:p>
<o:p> </o:p>
ErrHandler:<o:p></o:p>
MsgBox Err.Description<o:p></o:p>
Resume ExitHandler<o:p></o:p>
End Sub<o:p></o:p>
 
Upvote 0
Yes that's what I am looking for. Thank you much!!! I can actually manipulate it from here to work with my files. I just didn't know where to even start but this puts on the right track. Thanks a lot!! :)
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top