Avatar billede Bjerget Praktikant
25. september 2018 - 15:56

Special TXT fil til CSV fil

jeg har noget VBA code som jeg gerne vil ændre,
sådan at den tager alle TXT filer i en mappen på engang
og laver dem om til CSV filer, i stedet for at den kun tager en af gangen
som man vælger nu


Sub ConvertToCSV()
    Dim filePath: filePath = GetFilePath()
   
    If filePath <> "" Then
       
        Dim sCurrentLine, sTextHead As String, sText2 As String, iSectionLine As Integer
        Dim sText3 As String, sText4 As String, sText5 As String, sText6 As String
       
        Dim objFso As FileSystemObject: Set objFso = New FileSystemObject
        Set txtStream = objFso.OpenTextFile(filePath, ForReading, False)
       
        Dim baseName: baseName = objFso.GetBaseName(objFso.GetFile(filePath))
       
        ' Create a text file.
        Set tsFile = objFso.CreateTextFile(ThisWorkbook.path + "\CSV\" + (baseName) + ".CSV", True)
       
        Do While Not txtStream.AtEndOfStream
            sCurrentLine = txtStream.ReadLine
           
            If txtStream.Line = 4 Then
                sTextHead = sCurrentLine
            End If
           
            If (txtStream.Line > 9) Then
                If Left(sCurrentLine, 10) = "_______NEW" Then
                    iSectionLine = 0
                   
                    For iNumber = iStart To (iStart)
                        tsFile.WriteLine (baseName & ";" & sText2 & ";" & sText3 & ";" & sText4 & ";" & sText5 & ";" & sText6 & ";" & sTextHead & ";")
                    Next
                Else
                    iSectionLine = iSectionLine + 1
                   
                    If iSectionLine = 2 Then
                        sText2 = sCurrentLine
                    End If
                   
                    If iSectionLine = 3 Then
                        sText3 = sCurrentLine
                    End If
                   
                    If iSectionLine = 4 Then
                        sText4 = sCurrentLine
                    End If
                   
                    If iSectionLine = 5 Then
                        sText5 = sCurrentLine
                    End If
                   
                    If iSectionLine = 6 Then
                        sText6 = sCurrentLine
                    End If
                End If
            End If
        Loop
        ' Close data file.
        tsFile.Close
        txtStream.Close
       
        ' Create message.
        sMsg = "Konverteret til CSV-fil:" & vbNewLine & vbNewLine
        sMsg = sMsg & Trim(baseName) + ".CSV"
       
        ' Display message.
        MsgBox sMsg, vbInformation
    End If
End Sub

Function GetFilePath()
    ' Default return value.
    GetFilePath = ""

    ' Define the file dialog.
    Dim fileDialog As Office.fileDialog
   
    ' Create the file dialog.
    Set fd = Application.fileDialog(msoFileDialogFilePicker)
   
    With fd
   
        .AllowMultiSelect = False
       
        ' Set the title of the dialog box.
        .Title = "Please select the TXT-file."
       
        ' Clear out the current filters, and add our own.
        .Filters.Clear
        .Filters.Add "Data Files", "*.TXT"
        .Filters.Add "All Files", "*.*"
       
        ' Show the dialog box. If the .Show method returns True, the
        ' user picked at least one file. If the .Show method returns
        ' False, the user clicked Cancel.
        If .Show = True Then
            GetFilePath = .SelectedItems(1) 'replace txtFileName with your textbox
        End If
    End With
End Function
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester