Live-Forum - Die aktuellen Beiträge
Datum
Titel
28.03.2024 21:12:36
28.03.2024 18:31:49
Anzeige
Archiv - Navigation
1108to1112
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

CSV Import

CSV Import
Sven
Hallo,
ich habe ein kleines Problem.
Ich muss immer wieder CSV Dateien in Excel importieren.
Ich würde dies gerne per Makro immer machen und stelle mir das so in etwa vor:
Datei Öffnen Dialogfenster öffnet sich (Immer C:\temp Verzeichnis, da dort die Dateien sind)
Ich suche eine Datei aus, und diese wird mir importiert.
Die Kopfzeile sollte immer Fett und Zentriert sein und alle Spalten im Text-Format formatiert werden, da
dort Nummern mit ca. 18 Stellen sein werden.
Ist dies möglich? Ich habe keine Ahnung wie ich dies Anstellen soll. Könnt ihr mir helfen? BITTE!!
Gruß
Sven

6
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Benutzer
Anzeige
AW: CSV Import
22.10.2009 18:31:27
Tino
Hallo,
zeichne Dir den Import mal mit dem Rekorder auf wie Du es haben möchtest und
stell den Code hier rein, wir können Dir den Dialog dann einpflegen oder einbauen.
Gruß Tino
AW: CSV Import
22.10.2009 18:55:16
Sven

Sub Makro2()
' Makro2 Makro
' Makro am 22.10.2009 von Home aufgezeichnet
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\temp\ay9847_L5220_20091022114529.csv", Destination:=Range("A1"))
.Name = "ay9847_L5220_20091022114529"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1,  _
1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Rows("1:1").Select
Selection.Font.Bold = True
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Select
Selection.Columns.AutoFit
Range("A1").Select
End Sub

Der Name ist nicht immer gleich und alle Spalten sollten in Text formatiert sein (Spaltenanzahl variabel)
Gruß
Anzeige
AW: CSV Import
22.10.2009 19:21:44
Tino
Hallo,
kannst ja mal testen.
Sub Makro2()
Dim sPfad As String
Dim oQuery As QueryTable
If Dir("C:\temp", vbDirectory)  "" Then
ChDrive "C:"
ChDir "C:\temp"
sPfad = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
If sPfad  CStr(False) Then
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & sPfad, Destination:=Range("A1"))
.Name = "CSV-Import"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
For Each oQuery In ActiveSheet.QueryTables
oQuery.Delete
Next oQuery
With Rows("1:1")
.Font.Bold = True
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Cells.Columns.AutoFit
Range("A1").Select
End If
Else
MsgBox "Der Pfad 'C:\temp' wurde nicht gefunden!", vbCritical
End If
End Sub
Gruß Tino
Anzeige
AW: CSV Import
22.10.2009 19:35:39
Sven
Hi Tino,
vielen Dank schon mal!
Aber wie kann ich machen, dass alle verfügbaren (Keine feste Anzahl von Spalten) im Textformat formatiert werden?
Gruß
AW: CSV Import
22.10.2009 19:48:32
Tino
Hallo,
Formatiere Deine Zellen ins Textformat.
Sub Makro2()
Dim sPfad As String
Dim oQuery As QueryTable

If Dir("C:\temp", vbDirectory) <> "" Then
    ChDrive "C:"
    ChDir "C:\temp"
    
    sPfad = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
        
    If sPfad <> CStr(False) Then
        Cells.NumberFormat = "@" '<-----------------   Zellen in Textformat 
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & sPfad, Destination:=Range("A1"))
            .Name = "CSV-Import"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        
        For Each oQuery In ActiveSheet.QueryTables
         oQuery.Delete
        Next oQuery
        
        With Rows("1:1")
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
    
        Cells.Columns.AutoFit
        Range("A1").Select
    End If
Else
 MsgBox "Der Pfad 'C:\temp' wurde nicht gefunden!", vbCritical
End If
End Sub
Gruß Tino
Anzeige
AW: CSV Import
22.10.2009 20:53:00
Sven
Danke, hat alles wunderbar geklappt!
Vielen Dank

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige