Hi guys!
I use this macro in Excel to:
I use this macro in Excel to:
- open a FileDialog
- select some .txt files
- a copy of these files are created in a specific folder (empty at the beginning)
- a file named trend.txt has to be created in that folder. It is a merge of all .txt files.
- the problem is that in the file trend.txt created one of the .txt files appears two times. I need only one.
Code:
Sub PickAFile()
Dim fd As FileDialog
Dim Acti*******ed As Boolean
Dim LoopCounter As Long
Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.InitialFileName = "C:\Users"
fd.AllowMultiSelect = True
Acti*******ed = fd.Show
If Acti*******ed Then
For LoopCounter = 1 To fd.SelectedItems.Count
Call CreateCopyOfFile(fd.SelectedItems(LoopCounter))
Next LoopCounter
End If
End Sub
' NOW I CREATE A COPY OF ALL FILES
Sub CreateCopyOfFile(FilePathToCopy As String)
Dim fso As Scripting.FileSystemObject
Dim FileToCopy As Scripting.File
Dim ArchiveFolderPath As String
Set fso = New Scripting.FileSystemObject
ArchiveFolderPath = "C:\ProgramData\MyFolder\TrendFiles\"
If Not fso.FolderExists(ArchiveFolderPath) Then
fso.CreateFolder ArchiveFolderPath
End If
Set FileToCopy = fso.GetFile(FilePathToCopy)
FileToCopy.Copy ArchiveFolderPath & "\" & FileToCopy.Name
Set fso = Nothing
' NOW I CREATED A MERGE OF ALL FILES
c00 = "C:\ProgramData\MyFolder\TrendFiles\"
c01 = Dir(c00 & "*.txt")
Do Until c01 = ""
c02 = c02 & vbCrLf & CreateObject("scripting.filesystemobject").opentextfile(c00 & c01).readall
c01
' Now I create a merge of all files = Dir
Loop
CreateObject("scripting.filesystemobject").createtextfile(c00 & "trend.txt").write c02
End Sub