Mein Problem war es, eine CSV-Datei in Excel gespeichert, um tatsächlich zu öffnen, ohne irgendwelche Fehler, wenn ich versuche, es über VBA zu öffnen. Jedes Mal, wenn ich versucht habe, bekomme ich alle Spalten in einer einzigen Spalte. Wenn ich auf die Datei doppelklicke, öffnet sie sich als Text in einem Excel-Sheet mit 96 Spalten. Excel sortiert die Werte etwas, was ich nicht verstehe. Also wie kann ich das lösen?Teile von großen csv erhalten. als Excel gespeichert, um direkt in Excel zu öffnen
0
A
Antwort
0
Dies ist, was ich am Ende (mit viel Hilfe, die ich sagen muss), hoffentlich kann dies anderen helfen und es kann auch in anderen Einstellungen verwendet werden. Ich habe auch einen Hilfstext hinzugefügt.
So ist die Funktion und die Unter ive hinzugefügt zu einem Modul:
'Note the input parameters use ByVal and ByRef. ByVal means that I'm passing a variable by making a copy of it,
' and whatever I do with it in this routine doesn't affect the variable in the parent calling routine.
' ByRef means that the variable is that same variable that is being passed, not a copy of it, and so any
' changes to that object will still exist when returning to execution of the parent routine.
' As a rule, use ByVal unless you know that you want the parent routine to be aware of the changes to
' the variable, or if you are passing an object that you want to really make changes to, such as a worksheet.
'Writing to a worksheet cell by cell is quite slow, so we are going to speed things up by allowing the parent routine
' to specify which columns are wanted, using the "columnsWanted" input parameter
Public Sub import_csv_using_text_file_read_method(ByVal filePathName As String, _
ByRef outputSheet As Worksheet, _
Optional ByVal columnsWanted As String = "")
'clear output sheet, and select it
outputSheet.Cells.ClearContents
outputSheet.Select
'read file into an array of string type variables
Dim data() As String
data = readFileIntoStringArray(filePathName)
'now we are going to write this array to the spreadsheet
Dim outputRow As Long
outputRow = 0 '< this is the output row pointer, we will increment this as we go, so first increment will write to row 1
Dim i As Long '< array position pointer
Dim outputColumnIndex As Long '< output column
Dim cellData() As String, cellDataPos As Long 'we are going to "Split" the file line using semicolon, to give us an array of strings
Dim headers() As String 'keeping a copy of the column headers from the first row
' ...for each output cell, and we will use cellDataPos as a pointer to read from this array
For i = 0 To UBound(data) '< "Ubound" gives us the maximum index of the array, remember we index from zero, so if Ubound(myArray)=9 then we have 10 items
outputRow = outputRow + 1 '< incrementing the output row for every input file line
outputColumnIndex = 0 '< reset output column index pointer
Application.StatusBar = "Writing output: row " & Format(outputRow, "#,##0") & " (" & Format(i/UBound(data), "0.0 %") & ")" '< visual progress update
'now let's use the clever "Split" function
cellData = Split(data(i), ";")
'take a copy of these for "headers" if this is the first row
If i = 0 Then headers = cellData
'now iterate the input columns
For cellDataPos = 0 To UBound(cellData)
'we only care about certain columns, if the header exists in "columnsWanted" (if this is not specified, then return all columns)
If InStr(";" & LCase(columnsWanted) & ";", ";" & LCase(headers(cellDataPos)) & ";") > 0 Or columnsWanted = "" Then
'so we want this one, increment the output column index pointer
outputColumnIndex = outputColumnIndex + 1
'we are only going to bother writing the value if it has some content, let's not fire blanks!
If Len(Trim(cellData(cellDataPos))) > 0 Then
'actually write it
outputSheet.Cells(outputRow, outputColumnIndex).Value = Trim(cellData(cellDataPos))
End If
'note, I am using TRIM(variableName) to strip out any leading or trailing spaces from the data,
' that we can see in the text file in notepad
End If
Next
DoEvents '< a bit of grease
Next
'end
Application.StatusBar = ""
End Sub
-------------------------------------------
'*** routine to import the file into a string array ***
'
' ...note the extra brackets at the end of this function type definition, As String()
' this means it will return an array of type string, not just a single string
Private Function readFileIntoStringArray(ByVal filePathName As String) As String()
'declare the output array
Dim outputArray() As String
'I'm not going to "Redim" the array to a specific size, as I will increase it every time I write a string to it
'This following variable "arrayPos" I will use as a pointer to the last item in the array. On first use, this
'will increment to zero, ie. the first position in the array, for now I'm just getting it ready.
Dim arrayPos As Long
arrayPos = -1
'open the file, and read each line from it (easy code to lift from google)
Dim fileContentLine As String
Open filePathName For Input As #1
Do Until EOF(1)
Line Input #1, fileContentLine
'write this file content line to the output array
arrayPos = arrayPos + 1
Application.StatusBar = "Reading file: line " & Format(arrayPos + 1, "#,##0") '< visual progress update
If arrayPos = 0 Then
ReDim outputArray(0) 'the first time we "dimension" (set the size of) an array, we cannot use "Preserve"
Else
ReDim Preserve outputArray(arrayPos) 'when increasing the size of an existing array, we use "Preserve" to keep existing contents, not wipe them
End If
outputArray(arrayPos) = fileContentLine
Loop
Close #1
'return the outputArray
readFileIntoStringArray = outputArray
'end
Application.StatusBar = ""
End Function
Dann erstellt ive ein Modul mit dem nächsten Teil und den reff Zellen, in denen ich den Pfad zu dem Ordner und die CSV-Datei-Namen hinzufügen.
Public Sub ImportCSV_buttonClick()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'assemble a file path name, that includes the folder and file name
Dim FPN As String
With Sheets(".")
FPN = .Cells(2, 3).Value
If Right(FPN, 1) <> "\" Then FPN = FPN & "\"
FPN = FPN & .Cells(3, 3).Value
End With
'what columns do we want?
Dim columnsWeWant As String
columnsWeWant = "Art.nr form;name ;price;deliv;next deliv.;Status"
'import file using generic routine
csvImport.import_csv_using_text_file_read_method FPN, Sheets("output"), columnsWeWant
'End
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Dies zwingt die Datei in das Blatt, wo ich den Code und das Blatt „Output“ habe nur mit den Spalten ich von der großen csv benötigen.
BEARBEITEN Ein csv hat "," als seperator, während dies ";" Ursache für regionale Einstellungen. Dies kann immer noch mit dem Ändern dieses Teils verwendet werden.
Verwandte Themen
- 1. Öffnen von CSV-Dateien in Excel 2016
- 2. öffnen CSV-Datei mit Sonderzeichen in Excel
- 3. Kopieren der großen Daten von CSV zu Excel
- 4. Excel-Automatisierung: Öffnen Sie vorhandene Excel-Datei als "neu"
- 5. Excel zu .csv Problem
- 6. Gespeichert Excel mit Excel 2016 kann nicht
- 7. Excel VBA Öffnen 1 CSV-Datei
- 8. Öffnen von CSV-Dateien in MicroSoft Excel auf einem Mac
- 9. Streifen Teile Zeichenfolge in Excel
- 10. Excel in der Webseite öffnen?
- 11. Excel Formel, um die Werte zu erhalten
- 12. xlwt: Zahlen gespeichert als Zeichenfolge haben Dezimalpunkt in Excel
- 13. Sortierung einer großen Datenmenge Excel
- 14. Excel VBA öffnen Sie den Dialog zum Importieren von CSV
- 15. Wie CSV-Daten in Excel zu fett?
- 16. Excel-Arbeitsmappe von MemoryStream öffnen
- 17. So speichern Sie Excel-Datei als CSV?
- 18. Wie spss Dateien in Excel zu öffnen?
- 19. Excel Tabellenkalkulation Generation führt zu "anderen Dateiformat als Erweiterung Fehler" beim Öffnen in Excel 2007
- 20. Excel Ausgabe „in definierten Namen gespeichert 4.0 Funktion Excel“
- 21. Exportieren treetable zu Excel/CSV in adf
- 22. Excel - Vergleichen von zwei Spalten, um eine Differenz zu erhalten
- 23. öffnen zwei Excel-Dateien
- 24. Python Konvertieren von Excel in CSV
- 25. Import mehrzeiliger CSV-Dateien in Excel international
- 26. Import von CSV-Dateien in Excel
- 27. Excel Formel/SQL, um berechneten Wert in Access zu erhalten
- 28. Excel VBA: Öffnen und Lesen von Excel-Datei
- 29. Öffnen Sie Office XML Excel
- 30. Excel Export als HTML schlägt Grenzen in Excel 2016 nicht
CSV sind kommagetrennte Wertdatei. Es wird erwartet, dass ein Komma als Trennzeichen und kein Semikolon verwendet wird. Excel ersetzt manchmal das Komma durch Semikolons entsprechend den regionalen Einstellungen beim Exportieren nach CSV, aber ich denke, es sollte in Ihrer Antwort darauf hingewiesen werden, dass Sie eine bestimmte DSV-Datei und nicht eine echte CSV verwenden. –
Wenn Ihr Problem mit dem Semikolon auftritt, können Sie nicht die Methode 'Workbooks.OpenText' ([link] (https://msdn.microsoft.com/en-us/library/office/ff837097.aspx)) verwenden und angeben Dein Trennzeichen? – asongtoruin
@asongtoruin hat das versucht, hat nicht viel geholfen. Deshalb ist dieser "enorme" Code – krib