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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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