I get sent a folder containing zip files. I extract the zips to another folder. This then gives me 65 sub folders(directories), each with an excel and a text file. I wish to open all the excel files with one action in VB. If possible put them all on a single sheet in one workbook. They are all formatted the same and do not contain a lot of data so size and formatting will not be a problem.
I have - Sub Open_My_Files()
-
Dim MyFile As String
-
MyPath = "C:\Temp\Unzipped"
-
MyFile = Dir(MyPath)
-
-
Do While MyFile <> ""
-
If MyFile Like "*.xls" Then
-
Workbooks.Open MyPath & MyFile
-
End If
-
MyFile = Dir
-
Loop
-
End Sub
to extract from a single folder. I need to get it out of sub folders though.
Can anyone please help?
This is tested in VBA Excel 2003
Q1: how to add Reference:(see gif's)
In VBA: click "Tools" - "References"
Select: Microsoft Scripting Runtime
Q2: The error is because the Reference is not added.
Attached is a demo sheet.
PS: if it does not work in 2007, please let me know.
8 8593
What about copying / moving all the files to a single location.
What about copying / moving all the files to a single location.
Because it would be faster to open them individually. I want to open 65 files quickly and copy them all to the same sheet.
This goes to a folder and search each subfolder for an XLS file.
You have to add the reference= Microsoft Scripting Runtime
(c:\windows\system32\scrrun.dll) - Private Sub Com_all_files_and_subs_Click()
-
Dim FSO As New FileSystemObject
-
Dim START_FOLDER As Folder
-
Dim MyPath As String
-
MyPath = "C:\Temp\Unzipped"
-
MousePointer = 11
-
On Error GoTo Error_Files
-
Set START_FOLDER = FSO.GetFolder(MyPath)
-
Call Open_Files(START_FOLDER) '§ must be a function because it calls it's own
-
MousePointer = 0
-
Exit Sub
-
Error_Files:
-
MsgBox ("Error folder")
-
MousePointer = 0
-
End Sub
-
-
Private Function Open_Files(ByVal START_FOLDER)
-
Dim MyFile As File
-
Dim SUBFOLDER As Folder
-
For Each MyFile In START_FOLDER.Files
-
On Error GoTo Volgende
-
Text1.Text = Text1.Text & MyFile & vbNewLine
-
If MyFile Like "*.xls" Then
-
Workbooks.Open MyFile
-
End If
-
Volgende:
-
Next
-
For Each SUBFOLDER In START_FOLDER.SubFolders
-
Call Open_Files(SUBFOLDER)
-
Next
-
End Function
Guido, a couple of problems. VB is not one of my strengths and I have little understanding of it.
What do I have to do for this
You have to add the reference= Microsoft Scripting Runtime
(c:\windows\system32\scrrun.dll)
Also this is "Private Sub " so it does not show on my list of available macros. I can change it to "Sub" then it does appear but I then get the error - Compile error
-
-
User defined type not defined
and this code is highlighted on the debug - Dim FSO As New FileSystemObject
Also this is in excel 2007, if that makes a difference
This is tested in VBA Excel 2003
Q1: how to add Reference:(see gif's)
In VBA: click "Tools" - "References"
Select: Microsoft Scripting Runtime
Q2: The error is because the Reference is not added.
Attached is a demo sheet.
PS: if it does not work in 2007, please let me know.
Thanks a lot Guido. I figured out the Tools and References but could not get the Private sub to run without your User Form. Works a treat in 2007.
Now for my next question, can VB copy all the first sheets in those books opened to a single sheet in a new workbook?
I'll start working on it but you will probably beat me to the answer.
Thanks again
I have so far managed - Sub CopyALL()
-
ActiveSheet.Range("A1").CurrentRegion.Offset(1).Resize(Range("A1").CurrentRegion.Rows.Count - 1).Copy _
-
Destination:=Workbooks("copybook.xlsm").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
-
ActiveWorkbook.Close False
-
-
End Sub
Now I just need to get it to loop through this in your code Guido, if its possible. At the moment I have just associated it to a shortcut key so that I just keep pressing ctrl+g until I'm back to the "copybook" sheet where all the copies are.
[edit]Cracked it, goes between lines 24 and 25 of your code. Works a treat thanks[/edit]
this wiil collect all the first sheets and dump it in the workbook with the macro:(see also attachment) - Private Sub Com_all_files_and_subs_Click()
-
Dim FSO As New FileSystemObject
-
Dim START_FOLDER As Folder
-
Dim MyPath As String
-
Dim WORKBOOKSidx As Integer
-
Dim ARRCOLLECT As Variant
-
MyPath = "C:\Temp\Unzipped"
-
MousePointer = 11
-
On Error GoTo Error_Files
-
Set START_FOLDER = FSO.GetFolder(MyPath)
-
Call OPEN_FILES(START_FOLDER) '§ must be a function because it calls it's own
-
For WORKBOOKSidx = 2 To Workbooks.Count
-
Workbooks(WORKBOOKSidx).Activate
-
Sheets(1).Activate
-
ReDim ARRCOLLECT(0) '§ clear array
-
'§ set sheet1 to array
-
ARRCOLLECT = Range("A1").Resize(Range("A1").End(xlDown).Row, _
-
Range("A1").End(xlToRight).Column)
-
'§ dump array in sheet "collect"
-
Workbooks(1).Activate
-
Sheets(1).Activate
-
If Range("A1") = "" Then '§ blanco sheet
-
Range("A1").Resize(UBound(ARRCOLLECT, 1), _
-
UBound(ARRCOLLECT, 2)) = ARRCOLLECT
-
Else
-
Range("A" & Range("A1").End(xlDown).Row + 1). _
-
Resize(UBound(ARRCOLLECT, 1), _
-
UBound(ARRCOLLECT, 2)) = ARRCOLLECT
-
End If
-
Next
-
MousePointer = 0
-
Exit Sub
-
Error_Files:
-
MsgBox ("Error folder")
-
MousePointer = 0
-
End Sub
-
-
Private Function OPEN_FILES(ByVal START_FOLDER)
-
Dim MyFile As File
-
Dim SUBFOLDER As Folder
-
For Each MyFile In START_FOLDER.Files
-
On Error GoTo Volgende
-
Text1.Text = Text1.Text & MyFile & vbNewLine
-
If MyFile Like "*.xls" Then Workbooks.Open MyFile
-
Volgende:
-
Next
-
For Each SUBFOLDER In START_FOLDER.SubFolders
-
Call OPEN_FILES(SUBFOLDER)
-
Next
-
End Function
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Karuppasamy |
last post by:
Hi
I want to populate all the files and folders of System in a Treeview control
like Windows Explorer.
I try this using File System Objects. But sometimes I am getting an error
like 'Access...
|
by: Oci-One Kanubi |
last post by:
Everything works fine in Access, but when I double-click on the
resultant Excel files the first one opens correctly, but subsequent
ones, and any other Excel files I try to open, fail to display at...
|
by: Rajiv Das |
last post by:
VS 2003, XP SP2
------------------------------------------------------------
DirectoryInfo temporary = new
DirectoryInfo( Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));...
|
by: Neil |
last post by:
Hi
I'm rendering my datagrid to a HTML stream with a .xls extension to open in Excel however it opens in the browser. Is there a way to programatically force it to open Excel? I know you can...
|
by: Jon Maz |
last post by:
Hi All,
I'm in a possibly unusual situation, that is working alternately on an
ASP.NET web site from two dev computers, one of which has VS.Net installed
on it, the other of which does not. As...
|
by: Daniel |
last post by:
Hi all,
Can i open and edit the excel sheet on web page after downloading?
After editing, i close the web page and the excel file auto upload to the
server.
Is it possible? I really struggling...
|
by: semedao |
last post by:
Hi all,
I'm looking for some example , open source etc code that implement caching
of files and folders with those requirments:
1. like every file system , I can write , read , delete etc files...
|
by: karthi84 |
last post by:
hi,
if i have selected view hidden files and folders option from the folder option it is not enabled. at the time of applying it works but if i close that window and open it again its not...
|
by: chrisbirk |
last post by:
Hi!
I want to write an application on windows xp, which iterates n times a model script (exe) and copies every single run the results (multiple files such as text, maps, subfolders,etc) from the...
|
by: =?Utf-8?B?U3RldmU=?= |
last post by:
I have a C# application (2005 .Net) which will loop thru files in a directory
and read Excel files (currently using Excel 11). There are usually multiple
Excel files in the directory. Up until a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |