Hello all,
What is the most efficient way to code a macro to do something to every file in a folder? I've done my homework, and searched the threads. Here are some samplings of code I pulled from those posts
But what I really need is the most efficient method. There will be 2500 files in this folder. Luckily, I just need to open each txt file, save it as an xls
but I know that even the smallest coding change can greatly improve or hinder efficiency.
What can work for me? Shortest or most efficient code wins!
Thanks,
Dguenther
What is the most efficient way to code a macro to do something to every file in a folder? I've done my homework, and searched the threads. Here are some samplings of code I pulled from those posts
Code:
Option Explicit
Public Function BrowseForFolder(Optional ByVal StartFolder As Variant)
If IsMissing(StartFolder) Then StartFolder = ""
If StartFolder <> "" Then
If Right(StartFolder, 1) <> "\" Then StartFolder = StartFolder & "\"
End If
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = StartFolder
.Title = "Please choose a folder:-"
.AllowMultiSelect = False
If .Show = -1 Then BrowseForFolder = .SelectedItems(1)
End With
''''''''''''''''''''''''''''''''''''''''
End Function
'Get the folder object associated with the directory
Dim SelectedFolder As String
SelectedFolder = BrowseForFolder
If SelectedFolder = "" Then
' do whatever you need to do when user cancels out of dialog box
MsgBox "User cancelled!" & Space(10), vbOKOnly + vbExclamation
Exit Sub
End If
Set objFolder = objFSO.GetFolder(SelectedFolder)
'''''''''''''''''
'Dim xlApp as Excel.Application
If Right(sPath, 1) <> "\" Then
sPath = sPath & "\"
End If
'sets directory and grabs first filename
sFilename = Dir(sPath & "*.xlsx")
Do Until sFilename = ""
'Code here, to open it I used this...
Set xlApp = New Excel.Application
xlApp.Workbooks.Open (sPath & sFilename)
'More stuff
xlApp.Quit
sFilename = Dir
Loop
Set xlApp = Nothing
Code:
Workbooks.OpenText Filename:="WHATEVERTHEFILEISCALLED", _
Origin:=437, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
What can work for me? Shortest or most efficient code wins!
Thanks,
Dguenther