Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1492to1496
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

Tabellen zusammenführen

Tabellen zusammenführen
15.05.2016 18:49:49
Matze
Liebes Forum,
anbei mein Code (mit Hilfe von "Luschi"), wo die Tabellen 2,4 und 5 in Tabelle 6 zusammengefasst werden.
Wie muss ich den Code ergänzen, damit er immer die alten Inhalte aus Tabelle6 löscht und dann neu befüllt - also, dass die Daten immer aktuell sind. Momentan schreibt er mir die Zusammenfassungen in Tabelle6 immer untereinander, sobald man die Schaltfläche erneut drückt bzw. das Makro auslöst.
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow) 'Spalte A  _
kommt in Spalte A
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow) 'Spalten  _
B bis F kommen in Spalte G bis K
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur Zusammenschreiben"
Resume exit_here
End Sub

19
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Tabellen zusammenführen
15.05.2016 19:03:39
Werner
Hallo Matze,
nach Set wsTotal = Sheets("Tabelle6") habe ich im Code noch die Zeile wsTotal.UsedRange.Delete eingefügt.
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
wsTotal.UsedRange.Delete
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow) 'Spalte A   _
_
kommt in Spalte A
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow) 'Spalten  _
_
B bis F kommen in Spalte G bis K
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur Zusammenschreiben"
Resume exit_here
End Sub
Gruß Werner

Anzeige
AW: Tabellen zusammenführen
15.05.2016 19:58:35
Matze
Hallo Werner,
Dankeschön für die schnelle Antwort. Leider ordnet mir nun das Makro die Spaltenwerte der Tabellen 2,4 und 5 an die falschen Stellen in der Tabelle6.
Wäre sehr nett, wenn du es dir mal anschauen könntest.
Anbei meine Beispieldatei:
https://www.herber.de/bbs/user/105575.xlsm
Danke im Voraus

AW: Tabellen zusammenführen
15.05.2016 20:22:15
Werner
Hallo Matze,
hab momentan nur ein Tablet und kann keine Excel mit Makros öffnen.
Hast du im Blatt 6 Überschriften in Zeile 1 ?
Wie viele Spalten gibt es im Blatt 6 ?
Immer die gleiche Spaltenanzahl in Blatt 6 ?
Gruß Werner

Anzeige
AW: Tabellen zusammenführen
15.05.2016 20:37:30
Werner
Hallo Matze,
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
With wsTotal
.Range(.Cells(2, 1), .Cells(lngtotal, 15)).Delete
End With
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow) 'Spalte  _
A   _
_
kommt in Spalte A
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow) ' _
Spalten  _
_
B bis F kommen in Spalte G bis K
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Gruß Werner
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur  _
Zusammenschreiben"
Resume exit_here
End Sub

Anzeige
AW: Tabellen zusammenführen
15.05.2016 20:47:08
Werner
Hallo Matze,
hab noch was vergessen. Nach dem End With
With wsTotal
.Range(.Cells(2, 1), .Cells(lngTotal, 15)).Delete
End With
muss noch die Zeile
lngTotal = 2
rein.
Und im Code hatte ich die Variable falsch geschrieben:
falsch: lngtotal
richtig: lngtotal
Gruß Werner

AW: Tabellen zusammenführen
15.05.2016 20:55:04
Matze
Hallo Werner,
es wird leider nur die Tabelle5 in Tabelle6 übertragen:
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
With wsTotal
.Range(.Cells(2, 1), .Cells(lngTotalRow, 15)).Delete
End With
lngTotalRow = 2
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow)
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow)
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur_Zusammenschreiben"
Resume exit_here
End Sub

Anzeige
AW: Tabellen zusammenführen
15.05.2016 21:36:52
Werner
Hallo Matze,
stimmt nicht, es werden schon alle übertragen, nur werden die übertragenen Daten immer wieder gelöscht, wei ich das Löschen innerhalb der For - Next Schleife habe - mein Fehler.
Versuch mal so:
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row
With wsTotal
.Range(.Cells(2, 1), .Cells(lngTotalRow, 15)).Delete
End With
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow)
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow)
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur_Zusammenschreiben"
Resume exit_here
End Sub
Gruß Werner

Anzeige
AW: Tabellen zusammenführen
15.05.2016 21:46:02
Matze
Hallo Werner,
funktioniert perfekt - danke dir nochmal für deine Mühe.
Gruß Matze

AW: Gerne und danke für die Rückmeldung.
15.05.2016 21:49:35
Werner
Hallo Matze,
ich sollte vielleicht darauf verzichten das am Tablet zu machen.
Am Computer wäre mir das wahrscheinlich gleich aufgefallen und die Sache wäre schneller erledigt gewesen.
Aber wichtig ist ja nur, dass es jetzt geht.
Gruß Werner

AW: Tabellen zusammenführen
15.05.2016 23:07:43
Piet
Hallo Werner,
du warst etwas schneller, ich habe mir erlaubt deinen Code zu überarbeiten.
Nur aus Neugierde, wie gefaellt dir meine Überarbeitung? u.a. UsedRange.Delete
Ich habe noch die waagerechten und senkrechten Rahmen mit eingefügt.
Der Code ist und bleibt aber deine Idee. Ich habe nur korrigiert.
mfg Piet.
'Original Code von Werner  -  überarbeitet von Piet  15.5.2016
'geaenderte Teile:
'** Clear Used-Range ohne Ünerschriftzeile
'** neu eingefügt:  Rahmen setzen  (Linie)
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
'** Clear Used-Range ohne Ünerschriftzeile   (Piet)
wsTotal.UsedRange.Offset(1, 0).Delete
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow) 'Spalte A  _
kommt in Spalte A
.Range("B2:J10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow) 'Spalten  _
B bis F kommen in Spalte G bis O
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
'** Rahmen setzen  - neu eingefügt -   (Piet)
If ws.Name  "Tabelle5" Then
With wsTotal.Range("A1").End(xlDown).Resize(1, 15).Borders(xlEdgeBottom)
.LineStyle = xlDash
.Weight = xlThin
End With
End If
'** Rahmen setzen  - Ende -
End If
End With
End If
Next ws
'** Rahmen senkrecht  - neu eingefügt -  (Piet)
lngLastRow = wsTotal.Range("A1").End(xlDown).Row
With wsTotal.Range("A1:A" & lngLastRow)
.Offset(0, 0).Borders(xlEdgeRight).LineStyle = xlContinuous
.Offset(0, 5).Borders(xlEdgeRight).LineStyle = xlContinuous
End With
'** Rahmen senkrecht  - Ende -
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur Zusammenschreiben"
Resume exit_here
End Sub

Anzeige
AW: Tabellen zusammenführen
16.05.2016 13:29:59
Matze
Hallo Piet,
sieht auch super aus und funktioniert einwandfrei - vielen Dank euch beiden.
Gruß Matze

AW: Tabellen zusammenführen
16.05.2016 13:36:08
Matze
Hallo Piet und Werner,
noch eine "dumme" Frage. In den Spalte B bis F habe ich einen SVERWEIS welche sich auf Spalte A bezieht. Funktioniert auch alles, bloß ich muss ich immer manuell bis auf den letzten Eintrag in Spalte A hinunterziehen. Kann man sowas auch irgendwie automatisieren?
Danke

AW: Tabellen zusammenführen
16.05.2016 14:15:08
Werner
Hallo Matze,
ich gehe mal von folgenden Voraussetzungen aus.
1. Wir sprechen von Blatt 6
2. die Formeln stehen im Bereich B1 bis F1ws
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row
With wsTotal
.Range(.Cells(2, 1), .Cells(lngTotalRow, 15)).Delete
End With
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow)
.Range("B2:F10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow)
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0,  _
1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address). _
Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
End If
End With
End If
Next ws
With wsTotal
lngTotalRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("B1:F1").Copy
.Range("B2:F:" & lngTotalRow).PasteSpecial Paste:=xlPasteFormulas
Application.CutCopyMode = False
End With
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der  _
Prozedur_Zusammenschreiben"
Resume exit_here
End Sub

Gruß Werner

Anzeige
AW: Tabellen zusammenführen
16.05.2016 14:37:58
Matze
Hallo Werner,
ganz genau, so war es gedacht. Es kommt aber leider eine Fehlermeldung: "Fehler 1004: Die Methode 'Range für das Objekt _ Worksheets ist fehlgeschlagen in der Prozedur zusammenschreiben"
Sub Zusammenschreiben()
Dim ws As Worksheet
Dim wsTotal As Worksheet
Dim lngLastCol As Long
Dim lngLastRow As Long
Dim rngTotalFound As Range
Dim lngTotalRow As Long
Dim lngCounter As Long
Dim strSearch As String
Dim sTabs As String
'Tabellen, die zusammengeführt werden sollen
sTabs = "*Tabelle2*Tabelle4*Tabelle5*"
On Error GoTo Zusammenschreiben_Error
Application.ScreenUpdating = False
Set wsTotal = Sheets("Tabelle6")
'** Clear Used-Range ohne Ünerschriftzeile   (Piet)
wsTotal.UsedRange.Offset(1, 0).Delete
For Each ws In Worksheets
If InStr(1, sTabs, "*" & ws.Name & "*", vbTextCompare) > 0 _
Then
lngTotalRow = wsTotal.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lngTotalRow
With ws
lngLastRow = ws.Cells(Rows.Count, "A").End(xlUp).Row + 1
lngLastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
.Range("A2:A" & lngLastRow).Copy Destination:=wsTotal.Range("A" & lngTotalRow) 'Spalte A  _
_
kommt in Spalte A
.Range("B2:J10" & lngLastRow).Copy Destination:=wsTotal.Range("G" & lngTotalRow) 'Spalten  _
_
B bis F kommen in Spalte G bis O
If lngLastCol > 6 Then
For lngCounter = 7 To lngLastCol
strSearch = ws.Cells(1, lngCounter)
Set rngTotalFound = wsTotal.Rows("1:1").Find( _
what:=strSearch, LookIn:=xlValues, lookat:=xlWhole, _
searchorder:=xlByRows)
If rngTotalFound Is Nothing Then
Set rngTotalFound = wsTotal.Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1)
rngTotalFound.Value = strSearch
End If
.Range(Cells(2, lngCounter).Address, Cells(lngLastRow, lngCounter).Address).Copy _
Destination:=wsTotal.Cells(lngTotalRow, rngTotalFound.Column)
Next lngCounter
'** Rahmen setzen  - neu eingefügt -   (Piet)
If ws.Name  "Tabelle5" Then
With wsTotal.Range("A1").End(xlDown).Resize(1, 15).Borders(xlEdgeBottom)
.LineStyle = xlDash
.Weight = xlThin
End With
End If
'** Rahmen setzen  - Ende -
End If
End With
End If
Next ws
With wsTotal
lngTotalRow = .Cells(Rows.Count, "A").End(xlUp).Row
.Range("B1:F1").Copy
.Range("B2:F:" & lngTotalRow).PasteSpecial Paste:=xlPasteFormulas
Application.CutCopyMode = False
End With
'** Rahmen senkrecht  - neu eingefügt -  (Piet)
lngLastRow = wsTotal.Range("A1").End(xlDown).Row
With wsTotal.Range("A1:A" & lngLastRow)
.Offset(0, 0).Borders(xlEdgeRight).LineStyle = xlContinuous
.Offset(0, 5).Borders(xlEdgeRight).LineStyle = xlContinuous
End With
'** Rahmen senkrecht  - Ende -
exit_here:
Set rngTotalFound = Nothing
Application.ScreenUpdating = True
On Error GoTo 0
Exit Sub
Zusammenschreiben_Error:
MsgBox "Fehler " & Err.Number & " (" & Err.Description & ") in der Prozedur Zusammenschreiben"
Resume exit_here
End Sub
Danke und Gruß Matze

Anzeige
AW: Tabellen zusammenführen
16.05.2016 15:01:09
Werner
Hallo Matze,
da
.Range("B2:F:" & lngTotalRow).PasteSpecial Paste:=xlPasteFormulas
ist mir ein Doppelpunkt zu viel rein gerutscht.
Richtig ist das so:
.Range("B2:F" & lngTotalRow).PasteSpecial Paste:=xlPasteFormulas
Gruß Werner

AW: Tabellen zusammenführen
16.05.2016 15:05:32
Matze
Hallo Werner,
perfekt - danke dir nochmals :)

AW: Gerne und danke für die Rückmeldung. o.w.T.
16.05.2016 15:06:36
Werner

AW: Tabellen zusammenführen
16.05.2016 14:17:19
Piet
Hallo Matze,
ich habe aus dem Stegreif einen Zusatz Code entwickelt.
Man kann die Formeln aus Range B2:F2 per Makro nach unten kopieren.
Dazu gibt es zwei Varianten xlAll oder xlFormula. Testen welche besser ist.
Wenn es klappt die Lösch Routine aendern, damit Range B2:F2 immer stehen bleibt.
Ich hoffe meine Idee funktioniert. Würde mich freuen.
mfg Piet

'Lösch Routine bitte aendern
'** Clear Used-Range ohne Ünerschriftzeile   (Piet)
wsTotal.UsedRange.Offset(2, 0).Delete
wsTotal.Range("G2:O2").ClearContents
'diese Makros testen und ins Makro Tabelle 5 einfügen
'das Makro sollte vor - Rahmen eingefügen - stehen !!
Sub Spalte_B_bis_F_kopieren_xlAll()
lngLastRow = wsTotal.Range("A1").End(xlDown).Row
Range("B2:F2").Copy
Range("B2:F" & lngLastRow).PasteSpecial Paste:=xlPasteAll, Transpose:=False
Application.ScreenUpdating = False
End Sub
Sub Spalte_B_bis_F_kopieren_xlFormula()
lngLastRow = wsTotal.Range("A1").End(xlDown).Row
Range("B2:F2").Copy
Range("B2:F" & lngLastRow).PasteSpecial Paste:=xlPasteAll, Transpose:=False
Application.ScreenUpdating = False
End Sub
'getesteten Code in Tabelle 5 einfügen:
'** Formeln kopieren  - neu eingefügt -  (Piet)
lngLastRow = wsTotal.Range("A1").End(xlDown).Row
Range("B2:F2").Copy
Range("B2:F" & lngLastRow).PasteSpecial Paste:=xlPasteAll, Transpose:=False
Application.ScreenUpdating = False
'** Rahmen senkrecht  - neu eingefügt -  (Piet)
With wsTotal.Range("A1:A" & lngLastRow)
.Offset(0, 0).Borders(xlEdgeRight).LineStyle = xlContinuous
.Offset(0, 5).Borders(xlEdgeRight).LineStyle = xlContinuous
End With
'** Rahmen senkrecht  - Ende -

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige