TXT-Datei aus Excel aufrufen

  • #1
K

klexy

Bekanntes Mitglied
Themenersteller
Dabei seit
04.05.2002
Beiträge
802
Reaktionspunkte
0
Ort
Bayern
Ich habe 10 TXT-Dateien: output1.txt, output2.txt usw. Die rufe ich folgendermaßen auf:
Code:
Workbooks.OpenText FileName:=C:\siebel\temp\output1.txt, Origin:=  usw...
Workbooks.OpenText FileName:=C:\siebel\temp\output2.txt, Origin:=  usw...
Workbooks.OpenText FileName:=C:\siebel\temp\output3.txt, Origin:=  usw...
Workbooks.OpenText FileName:=C:\siebel\temp\output4.txt, Origin:=  usw...
Dann kopier ich alles aus 10 unten dran in 9, dies alles unten dran in 8 usw. Jede enthält nur Spalte A und Spalte B.
Code:
    Windows(output10.txt).Activate
    Range(A2).Select
    Range(ActiveCell, ActiveCell.End(xlDown).Offset(0, 1)).Select
    Selection.Copy
    Windows(output9.txt).Activate
    Selection.End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste
    Range(A2).Select
    Range(ActiveCell, ActiveCell.End(xlDown).Offset(0, 1)).Select
    Selection.Copy
    Windows(output8.txt).Activate
    Selection.End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste
    usw...
Am Schluß hab ich dann alles in Datei output1.txt und so soll es sein.

Wenn ich jetzt aber nur 8 statt 10 Dateien habe, gibt es eine Fehlermeldung, weil er nach der 9 und nach der 10 sucht.

Gibt es etwas wie:
Code:
If exist output10.txt Then
Workbooks.OpenText FileName:=C:\siebel\temp\output10.txt, Origin:=  usw...
Else 
mach weiter mit output9.txt usw.

? ???
 
  • #2
Hi klexy,

zum Nikolaus ein Beispiel wie gewünscht ;D

Gruß Matjes ;)
Code:
Sub MeinProgramm()
  Dim s_tmp As String
  
  s_tmp = c:\download\xxxx.txt
  
  If ExistiertDatei(s_tmp) Then
    MsgBox (Datei  & s_tmp &  existiert   :-))
  Else
    MsgBox (Datei  & s_tmp &  existiert nicht!  :-()
  End If
End Sub

Function ExistiertDatei(s_kompletterPfad As String) As Boolean
  If Dir(s_kompletterPfad) =  Then
    ExistiertDatei = False
  Else
    ExistiertDatei = True
  End If
End Function
 
  • #3
Weil der Bub eben das ganze Jahr schön brav war... ;)

Das habe ich jetzt daraus gemacht:
Code:
If Dir(C:\siebel\temp\output1.txt) <>  Then
    Workbooks.OpenText FileName:=C:\siebel\temp\output1.txt, Origin:=  usw...
End If
If Dir(C:\siebel\temp\output2.txt) <>  Then
    Workbooks.OpenText FileName:=C:\siebel\temp\output2.txt, Origin:=  usw...
End If
If Dir(C:\siebel\temp\output3.txt) <>  Then
    Workbooks.OpenText FileName:=C:\siebel\temp\output3.txt, Origin:=  usw...
End If
usw...
und danach 10 in 9, 9 in 8, 8 in 7 usw. kopieren:
Code:
If Dir(C:\siebel\temp\output10.txt) <>  Then
    Windows(output10.txt).Activate
    Range(A2).Select
    Range(ActiveCell, ActiveCell.End(xlDown).Offset(0, 1)).Select
    Selection.Copy
End If
If Dir(C:\siebel\temp\output9.txt) <>  Then
    Windows(output9.txt).Activate
    If Dir(C:\siebel\temp\output10.txt) <>  Then
        Range(A2).Select
        Selection.End(xlDown).Offset(1, 0).Select
        ActiveSheet.Paste
    End If
    Range(A2).Select
    Range(ActiveCell, ActiveCell.End(xlDown).Offset(0, 1)).Select
    Selection.Copy
End If
If Dir(C:\siebel\temp\output8.txt) <>  Then
    Windows(output8.txt).Activate
    If Dir(C:\siebel\temp\output9.txt) <>  Then
        Range(A2).Select
        Selection.End(xlDown).Offset(1, 0).Select
        ActiveSheet.Paste
    End If
    Range(ActiveCell, ActiveCell.End(xlDown).Offset(0, 1)).Select
    Selection.Copy
End If
usw...
In diesem Sinne: Frohe Ostern! :D
 
Thema:

TXT-Datei aus Excel aufrufen

ANGEBOTE & SPONSOREN

Statistik des Forums

Themen
113.838
Beiträge
707.959
Mitglieder
51.491
Neuestes Mitglied
haraldmuc
Oben