View Full Version : Solved: How to open all files in folder using input box
annemarie
08-12-2009, 09:56 PM
Hi there!
I want to make a macro file where in I can open all files in a folder. I want that the user will be the one to input the folder path using input box.
Hope you could help me. I would appreciate it.
Thanks.
annemarie: pray2:
Greetings Annemarie,
When you say "all files," do you mean all Excel workbooks, or all files regardless of application?
Mark
mikerickson
08-12-2009, 10:47 PM
Application.GetOpenFileName is designed for getting file paths. An InputBox is not and requires more handling for user error.
annemarie
08-12-2009, 10:48 PM
Hi Mark,
To be specific *.csv files and *.dat files.
Thanks..
annemarie
08-12-2009, 10:55 PM
What I want to do here is for example a user will click a command button an input box will appear asking him to input the path of the folder which contains the files he wanted to open.Then the macro will open all the files on that folder.
Here is very basic code to use an InputBox.
Option Explicit
Sub OpenAllWorkbooks_InputBox()
Dim strPath As String
Dim strDefPath As String
Dim strFileName As String
strDefPath = Application.DefaultFilePath & Application.PathSeparator
strPath = InputBox("Enter full path...", "", _
Application.DefaultFilePath & Application.PathSeparator)
If Not strPath = "" Then
If Not Right(strPath, 1) = Application.PathSeparator Then
strPath = strPath & Application.PathSeparator
End If
Else
Exit Sub
End If
strFileName = Dir(strPath, vbNormal)
Do While Not strFileName = vbNullString
If Right(strFileName, 3) = "csv" _
Or Right(strFileName, 3) = "dat" Then
Workbooks.Open strPath & strFileName
End If
strFileName = Dir()
Loop
End Sub
Now personally, this would scare me a bit to use, as what happens when the user picks a folder with a bajillion files?
Did you take note of Mike's comment? If you used GetOpenFilename, the user could click on all the filenames they wanted to open, but this would eliminate the user from easily "picking" a wrong folder.
Mark
annemarie
08-12-2009, 11:42 PM
I think I could use this. Thanks for the help.'Til next time..
mdmackillop
08-13-2009, 12:04 AM
Hi annemarie,
Welcome to VBAX.
If this is Solved, please mark it so using the Thread Tools dropdown.
Regards
MD
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.