Code
Verfasst: Di 24. Mai 2011, 10:37
Was ist an diesem Code falsch?
Welchen Dateityp könnte man statt Document oder Workbook nehmen?
Sub Import()
Dim i As Integer
Dim fFile1 As Variant, fFile2 As Variant, neuFile As Variant
Dim Datei1 As Document, Datei2 As Document, neuDatei As Document
Dim WS1 As Worksheet, WS2 As Worksheet
'On Error GoTo ErrorHandler
' Datei 1 öffnen
fFile1 = Application.GetOpenFilename("CSV-Report (*.csv), *.csv")
If fFile1 = False Then
If Workbooks.Count = 1 Then
Application.Quit
Exit Sub
Else
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Open FileName:=fFile1
Set Datei1 = ActiveWorkbook
Set WS1 = Datei1.Worksheets(1)
WS1.Name = "Pattern_short"
' Datei 2 öffnen
fFile2 = Application.GetOpenFilename("CSV-Report (*.csv), *.csv")
If fFile2 = False Then
MsgBox "Der Vorgang wird vorzeitig abgebrochen."
If Workbooks.Count = 2 Then
Application.Quit
Exit Sub
Else
Datei1.Close , False
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Open FileName:=fFile2
Set Datei2 = ActiveWorkbook
Set WS2 = Datei1.Worksheets(1)
WS2.Name = "Pattern_long"
' Speicherort/-name neue Datei abfragen
neuFile = Application.GetSaveAsFilename("Zusammenfassung.xls")
If neuFile = False Then
MsgBox "Der Vorgang wird vorzeitig abgebrochen."
If Workbooks.Count = 3 Then
Application.Quit
Exit Sub
Else
Datei1.Close , False
Datei2.Close , False
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Add
ActiveWorkbook.SaveAs neuFile
Set neuDatei = ActiveWorkbook
' Blätter in die neue Datei kopieren
Datei2.Worksheets(1).Copy Before:=neuDatei.Sheets(1)
Datei1.Worksheets(1).Copy Before:=neuDatei.Sheets(1)
' Datei1 und 2 schliessen
Application.DisplayAlerts = False
Datei1.Close , False
Datei2.Close , False
Application.DisplayAlerts = True
' Unnötige Blätter löschen
If neuDatei.Worksheets.Count > 2 Then
For i = Worksheets.Count To 3 Step -1
Application.DisplayAlerts = False
Worksheets(i).Delete
Application.DisplayAlerts = True
Next i
End If
' neue Datei speichern
neuDatei.Save
' Vorlagedatei schliessen
ThisWorkbook.Saved = True
ThisWorkbook.Close
'' Switch on screenupdating
'Application.ScreenUpdating = True
'Exit Sub
'
'ErrorHandler:
'MsgBox "Unexpected Error"
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub
Liebe Grüße, Johanna
Welchen Dateityp könnte man statt Document oder Workbook nehmen?
Sub Import()
Dim i As Integer
Dim fFile1 As Variant, fFile2 As Variant, neuFile As Variant
Dim Datei1 As Document, Datei2 As Document, neuDatei As Document
Dim WS1 As Worksheet, WS2 As Worksheet
'On Error GoTo ErrorHandler
' Datei 1 öffnen
fFile1 = Application.GetOpenFilename("CSV-Report (*.csv), *.csv")
If fFile1 = False Then
If Workbooks.Count = 1 Then
Application.Quit
Exit Sub
Else
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Open FileName:=fFile1
Set Datei1 = ActiveWorkbook
Set WS1 = Datei1.Worksheets(1)
WS1.Name = "Pattern_short"
' Datei 2 öffnen
fFile2 = Application.GetOpenFilename("CSV-Report (*.csv), *.csv")
If fFile2 = False Then
MsgBox "Der Vorgang wird vorzeitig abgebrochen."
If Workbooks.Count = 2 Then
Application.Quit
Exit Sub
Else
Datei1.Close , False
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Open FileName:=fFile2
Set Datei2 = ActiveWorkbook
Set WS2 = Datei1.Worksheets(1)
WS2.Name = "Pattern_long"
' Speicherort/-name neue Datei abfragen
neuFile = Application.GetSaveAsFilename("Zusammenfassung.xls")
If neuFile = False Then
MsgBox "Der Vorgang wird vorzeitig abgebrochen."
If Workbooks.Count = 3 Then
Application.Quit
Exit Sub
Else
Datei1.Close , False
Datei2.Close , False
ThisWorkbook.Close
Exit Sub
End If
End If
Workbooks.Add
ActiveWorkbook.SaveAs neuFile
Set neuDatei = ActiveWorkbook
' Blätter in die neue Datei kopieren
Datei2.Worksheets(1).Copy Before:=neuDatei.Sheets(1)
Datei1.Worksheets(1).Copy Before:=neuDatei.Sheets(1)
' Datei1 und 2 schliessen
Application.DisplayAlerts = False
Datei1.Close , False
Datei2.Close , False
Application.DisplayAlerts = True
' Unnötige Blätter löschen
If neuDatei.Worksheets.Count > 2 Then
For i = Worksheets.Count To 3 Step -1
Application.DisplayAlerts = False
Worksheets(i).Delete
Application.DisplayAlerts = True
Next i
End If
' neue Datei speichern
neuDatei.Save
' Vorlagedatei schliessen
ThisWorkbook.Saved = True
ThisWorkbook.Close
'' Switch on screenupdating
'Application.ScreenUpdating = True
'Exit Sub
'
'ErrorHandler:
'MsgBox "Unexpected Error"
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub
Liebe Grüße, Johanna