2017-06-07 3 views
0

Bitte helfen Sie mir. Ich versuche, ein .xls/.xlsx auf einem bestimmten Pfad auf einem Server (sagen wir eine \ test \ 1.xls) Datei in .csv (ohne Beschädigung des Datumsformats oder andere Informationen) zu konvertieren und zu speichern in einem festen Pfad (sagen wir eins \ result \ 1.csv). Dieser Vorgang muss sich automatisch alle 24 Stunden wiederholen.VBA: konvertieren xls zu csv und speichern Sie es in einem bestimmten Pfad

Vielen Dank!

+1

Können Sie uns zeigen, welchen Code Sie bis jetzt haben und wo Sie stecken bleiben? Ist es das Speichern als? Ist es das Dateiformat? Ist es die Automatisierung? Gerade jetzt ist Ihre Frage zu weit gefasst, weil Sie keinen Code zeigen und nach drei verschiedenen Aspekten fragen. – teylyn

Antwort

0

Bis jetzt habe ich nur den Code unten, der nur in CSV konvertiert und speichert es an der gleichen Stelle wie die ursprüngliche Datei.
Auch aus irgendeinem unbekannten Grund konvertiert HALF der Daten zum 31.05.2017 für Beispiel (und das ursprüngliche Format ist 31.05.2017).

Sub ConvertXLSToCSV() 

    Dim InputPath As String 

    Dim PowershellCommand As String 



    ' To suppress excel prompts and alert messages while the macro is running. 

    Application.DisplayAlerts = False 

    Do 

     ' Taking the input excel file path which needs to be converted to .csv format. 

     InputPath = InputBox("Enter the full path of the input excel file.", "Convert XLS to CSV") 

     If Trim(InputPath) <> "" Then 

      If Dir(InputPath) = vbNullString Then 

       MsgBox "File: '" & InputPath & "' doesn't exists.", vbOKOnly + vbCritical, "Convert XLS to CSV" 

      ElseIf Split(Dir(InputPath), ".")(1) = "xlsx" Or Split(Dir(InputPath), ".")(1) = "xls" Then 

       InputPath = Trim(InputPath) 

       ' Opening the input excel file and saving it in .csv using powershell. 

       PowershellCommand = "$ExcelWB = new-object -comobject excel.application" & vbNewLine 

       PowershellCommand = PowershellCommand & "$ExcelWB = new-object -comobject excel.application" & vbNewLine 

       PowershellCommand = PowershellCommand & "$ExcelWB.Visible = $false" & vbNewLine & "$ExcelWB.DisplayAlerts = $false" & vbNewLine 

       PowershellCommand = PowershellCommand & "$Workbook = $ExcelWB.Workbooks.Open('" & InputPath & "')" & vbNewLine 

       PowershellCommand = PowershellCommand & "$Workbook.SaveAs('" & Left(InputPath, (InStrRev(InputPath, ".", -1, vbTextCompare) - 1)) & ".csv',6)" & vbNewLine 

       PowershellCommand = PowershellCommand & "$Workbook.Close($false)" & vbNewLine 

       PowershellCommand = PowershellCommand & "$ExcelWB.quit()" & vbNewLine 

       PowershellCommand = "Powershell.exe -command " & PowershellCommand 

       Set WshShell = CreateObject("WScript.Shell") 

       WshShell.Exec (PowershellCommand) 

       Exit Do 

      Else: 

       MsgBox "Input file is not in excel (.xlsx/.xls) format.", vbOKOnly + vbCritical, "Convert XLS to CSV" 

      End If 

     ElseIf StrPtr(InputPath) <> 0 Then MsgBox "Please enter the full path of the input excel file.", vbOKOnly + vbExclamation, "Convert XLS to CSV" 

     End If 

    Loop Until StrPtr(InputPath) = 0 

End Sub 
Verwandte Themen