Wrong merge of .txt files

assurdo

New Member
Joined
Sep 22, 2014
Messages
13
Hi guys!

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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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