CSV durchsuchen (InStr?)
25.10.2012 08:27:02
Manuela
Ich möchte vor dem einlesen einer CSV prüfen ob in der 1. Zeile ein bestimmtes Wort vorkommt. In einem CSV-Importmakro habe ich versucht dies mit Hilfe von InStr zu realisieren, aber irgendwie funktioniert es nicht... mhhh! Hat jemand eine Idee was hier falsch sein könnte?
Sub CSVImportAufteilen()
'CSV-Import
Dim strSrcFile$, strTmp$, strDelimit$
Dim intFile%, i&, k&, arrSrc
Dim lngLast As Long
'Öffnen-Dialog
strDelimit = ";"
intFile = FreeFile
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Datei wählen"
.InitialFileName = ""
.Filters.Add "CSV-Dateien", "*.csv", 1
.Filters.Add "Alle Dateien", "*.*", 2
If .Show Then
strSrcFile = .SelectedItems(1)
End If
End With
' Suche
Dim suche As String
suche = "Bratwurst"
If InStr(1, strSrcFile, suche) > 0 Then
Range("A1").Value = "Gefunden!"
Else
Range("A1").Value = "nicht gefunden!"
End If
'Datenimport
If strSrcFile "" Then
Open strSrcFile For Input As #intFile
Do While Not EOF(intFile)
Line Input #intFile, strTmp
If lngLast > 0 Then
arrSrc = Split(strTmp, strDelimit)
With ActiveSheet
lngLast = .Cells(Rows.Count, 1).End(xlUp).Row + 1
lngLast = Application.Max(lngLast, 4)
' ExcelSpalte = CSVSpalte
Cells(lngLast, 1) = arrSrc(0)
Cells(lngLast, 2) = arrSrc(1)
Cells(lngLast, 4) = arrSrc(2)
Cells(lngLast, 5) = arrSrc(3)
End With
End If
lngLast = lngLast + 1
Loop
Close
End If
End Sub
lg,Manu