AW: alle *.txt Files in einem Ordner öffnen und ...
06.05.2008 16:36:00
Peter
Hallo Rudi
Vielen Dank. Ich habe versucht, den Code auf meine Verhältnisse zu adaptieren.
Da ich in meinen Files verschiedene Tabulatoren setzen muss, habe ich ein separates Makro geschrieben, das mir die spezifische Umwandlung (Text in Spalten) bewerkstelligt. Deshalb habe ich verschiedene Zeilen deines Makro (sind es wohl die richtigen?) auskommentiert und anstelle dessen meinen Makro aufgerufen (mit Call AAStammdatenFile).
Im Moment scheint der Code ein Problem in der Zeile
.Calculation = IIf(Modus = True, xlManual, intCalculation)
zu haben - da springt er immer zur Fehlerbehandlung.
Da bin ich leider am Ende meines Lateins.
Kannst du mir allenfalls weiterhelfen?
- habe ich die richtigen Zeilen auskommentiert?
- woran könnte das Problem liegen, dass der Code bei .Calculation zur Fehlerbehandlung springt?
Danke für jede Hilfe.
Gruss, Peter
Option Explicit
Public intCalculation As Integer
Sub TXT2XLS()
'Alle .txt (Trennzeichen Tab) eines Ordners in .xls umwandeln
Dim oFS As Object, oFolder As Object, oFile As Object
Dim strFolder As String
Dim strTxt As String, myArr, lngL As Long, WKS As Worksheet, iFREE As Integer
With Application.FileDialog(4)
.InitialFileName = "H:\"
.InitialView = 2
.Title = "Bitte einen Ordner wählen"
If .Show = -1 Then
strFolder = .SelectedItems(1)
Else
Exit Sub
End If
End With
On Error GoTo FEHLER
DoEvents
GetMoreSpeed
Set oFS = CreateObject("scripting.filesystemobject")
Set oFolder = oFS.getfolder(strFolder)
iFREE = FreeFile
For Each oFile In oFolder.Files
If oFile.Name Like "*.txt" Then
lngL = 1
Open oFile For Input As iFREE
''''''' Set WKS = Workbooks.Add(1).Sheets(1)
''''''' Do Until EOF(iFREE)
''''''' Line Input #iFREE, strTxt
''''''' myArr = Split(strTxt, vbTab) 'Trennzeichen ; anpassen
''''''' With WKS
''''''' .Range(.Cells(lngL, 1), .Cells(lngL, UBound(myArr) + 1)) = myArr
''''''' End With
''''''' lngL = lngL + 1
''''''' Erase myArr
''''''' Loop
''''''' Close #iFREE
End If
Call AAStammdatenFile
''''''' With WKS.Parent
''''''' .SaveAs Replace(oFile, ".txt", ".xls"), xlWorkbookNormal
''''''' .Close False
''''''' End With
Set WKS = Nothing
Next oFile
AUFRAEUMEN:
Set oFile = Nothing
Set oFolder = Nothing
Set oFS = Nothing
GetMoreSpeed False
Exit Sub
FEHLER:
If Err.Number Then
MsgBox "Fehler!" & vbLf & Err.Description
Err.Clear
Resume AUFRAEUMEN
End If
End Sub
Sub GetMoreSpeed(Optional ByVal Modus As Boolean = True)
If Modus = True Then intCalculation = Application.Calculation
With Application
.ScreenUpdating = Not Modus
.EnableEvents = Not Modus
.Calculation = IIf(Modus = True, xlManual, intCalculation)
.Cursor = IIf(Modus = True, 2, -4143)
End With
End Sub
Sub AAStammdatenFile()
' Tastenkombination: Strg+Umschalt+J
Dim strName As String
Dim strPath As String
Dim strFull As String
strName = ActiveWorkbook.Name
strPath = ActiveWorkbook.Path
strFull = strPath & "\" & strName
strFull = WorksheetFunction.Substitute(strFull, ".txt", ".xls")
'MsgBox strFull
Application.Goto Reference:="C1"
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(20, 1), Array(26, 1), Array(47, 1), Array(67, 1), _
Array(76, 1), Array(87, 1), Array(109, 1), Array(136, 1), Array(152, 1)), _
TrailingMinusNumbers:=True
Columns("D:D").EntireColumn.AutoFit
ActiveWorkbook.Sheets(1).Name = "ValStammDaten"
ActiveWorkbook.SaveAs Filename:=strFull _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
ActiveWindow.Close
End Sub