Hallo peyd,
was mir nur aufgefallen ist, dass die Zuordnung der eingelesenen Datei beim Zurückschreiben in die Datei nicht stimmt. Das habe ich verbessert:
[code]Sub Makro_Giesserei()
Dim rCell As Range
Dim rRng As Range
Dim lzeile As Long
Dim ArrayC() As Variant
Dim zeile As Long
Dim i As Long
Dim j As Long
Dim pruef As Boolean
Dim z As Long
Dim s As Long
'Bildschirmaktualisierung ausschalten:
Application.ScreenUpdating = False
'Anzahl der Datensätze in Tabelle Giesserei ermitteln
lzeile = Sheets("Giesserei").Cells(Rows.Count, 2).End(xlUp).Row
'prüfen, ob letzte Zeile größer 4
If lzeile < 5 Then
pruef = False
Else
pruef = True
End If
If pruef = True Then
'Feld neu dimensionieren
ReDim ArrayC(lzeile - 4, 7)
'Daten aus Tabelle Giesserei, Spalte C in das Feld einlesen
For zeile = 5 To lzeile Step 4 ' ab Zeile 5 alle 4 Zeilen wiederholen
ArrayC(zeile - 4, 0) = Worksheets("Giesserei").Cells(zeile, 1).Value 'Name in Array schreiben
For z = 0 To 3
For s = 1 To 7
ArrayC(zeile - 4 + z, s) = Worksheets("Giesserei").Cells(zeile + z, 2 + s).Value 'Werte der Spalten C bis I in Array schreiben
Next s
Next z
Next zeile
'Nun Inhalt des Blattes Giesserei löschen
lzeile = Worksheets("Giesserei").Cells(Rows.Count, 2).End(xlUp).Row
With Worksheets("Giesserei")
.Range(.Cells(5, 1), .Cells(lzeile, 3)).EntireRow.Delete
End With
End If
'Tabellenblatt Giesserei neu aufbauen
Set rRng = Worksheets("Verkaufsgruppen").Range("D3:D215")
'Zellen im Bereich nach Ja durchsuchen
For Each rCell In rRng.Cells
'Prüfen ob Zellinhalt Ja ist
If rCell.Value = "Ja" Then
'falls ja, dann
With Sheets("Giesserei")
.Range("A5:A8").EntireRow.Insert 'Zeilen einfügen
.Cells(5, 2) = "Anlaufkosten"
.Cells(6, 2) = "VOK" 'Text einfügen
.Cells(7, 2) = "BEMI"
.Cells(8, 2) = "Invest"
.Cells(5, 10) = "=SUM(RC[-7]:RC[-1])" 'Zeilensummen Spalte J bilden
.Cells(6, 10) = "=SUM(RC[-7]:RC[-1])"
.Cells(7, 10) = "=SUM(RC[-7]:RC[-1])"
.Cells(8, 10) = "=SUM(RC[-7]:RC[-1])"
With .Range("A5:A8") 'Spalte A formatieren
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.ReadingOrder = xlContext
.MergeCells = True
End With
'Name aus dem Blatt Verkaufsgruppen einfügen
.Range("A5") = Worksheets("Verkaufsgruppen").Cells(rCell.Row, 2)
End With
End If
Next rCell
'letzte beschriebene Zeile in Spalte B ermitteln
lzeile = Worksheets("Giesserei").Cells(Rows.Count, 2).End(xlUp).Row
'Vorhandene Daten ggf. wieder in Tabelle Giesserei schreiben
If pruef = True Then
zeile = 5
For j = 1 To lzeile Step 4
For i = 1 To UBound(ArrayC) Step 4
If Worksheets("Giesserei").Cells(zeile, 1).Value = ArrayC(i, 0) Then
For z = 0 To 3
For s = 1 To 7
Worksheets("Giesserei").Cells(zeile + z, 2 + s) = ArrayC(i + z, s) 'Werte der Spalten C bis I aus Array in Tabelle schreiben
Next s
Next z
End If
Next i
zeile = zeile + 4
Next j
End If
'Summewenn-Formeln einfügen; letzte Zeilen in Tabelle Giesserei
With Sheets("Giesserei")
.Cells(lzeile + 1, 3).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";C5:C" & lzeile & ")"
.Cells(lzeile + 2, 3).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";C5:C" & lzeile & ")"
.Cells(lzeile + 3, 3).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";C5:C" & lzeile & ")"
.Cells(lzeile + 4, 3).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";C5:C" & lzeile & ")"
.Cells(lzeile + 1, 4).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";D5:D" & lzeile & ")"
.Cells(lzeile + 2, 4).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";D5:D" & lzeile & ")"
.Cells(lzeile + 3, 4).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";D5:D" & lzeile & ")"
.Cells(lzeile + 4, 4).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";D5:D" & lzeile & ")"
.Cells(lzeile + 1, 5).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";E5:E" & lzeile & ")"
.Cells(lzeile + 2, 5).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";E5:E" & lzeile & ")"
.Cells(lzeile + 3, 5).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";E5:E" & lzeile & ")"
.Cells(lzeile + 4, 5).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";E5:E" & lzeile & ")"
.Cells(lzeile + 1, 6).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";F5:F" & lzeile & ")"
.Cells(lzeile + 2, 6).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";F5:F" & lzeile & ")"
.Cells(lzeile + 3, 6).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";F5:F" & lzeile & ")"
.Cells(lzeile + 4, 6).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";F5:F" & lzeile & ")"
.Cells(lzeile + 1, 7).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";G5:G" & lzeile & ")"
.Cells(lzeile + 2, 7).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";G5:G" & lzeile & ")"
.Cells(lzeile + 3, 7).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";G5:G" & lzeile & ")"
.Cells(lzeile + 4, 7).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";G5:G" & lzeile & ")"
.Cells(lzeile + 1, 8).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Anlaufkosten"";H5:H" & lzeile & ")"
.Cells(lzeile + 2, 8).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""VOK"";H5:H" & lzeile & ")"
.Cells(lzeile + 3, 8).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""BEMI"";H5:H" & lzeile & ")"
.Cells(lzeile + 4, 8).FormulaLocal = "=SUMMEWENN(B5:B" & lzeile & ";""Invest"";H5:H"