Coding aus Datei2.XLSM (Komplett) --> auch mit SAP-Funktionen --> und es funktioniert alles, wenn ich die Datei2.XLSM manuell öffne und speicher
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
Dim Celle As String
Dim iresult As String
Dim Jahr1 As String
Dim Jahr1Found As Boolean
Dim Jahr2 As String
Dim Jahr2Found As Boolean
Dim SelectJahr As String
Dim Formel As String
Dim StrAdressR1C1 As Range
Dim MySheet As Worksheet
Dim wb As Workbook
' Erstes Jahr
Range("A10").FormulaR1C1 = "=MID(SAPGetDimensionEffectiveFilter(""DS_1"", ""0CALYEAR""),1,4)"
Jahr1 = Range("A10").Value
' Zweites Jahr
Range("A10").FormulaR1C1 = "=MID(SAPGetDimensionEffectiveFilter(""DS_1"", ""0CALYEAR""),7,4)"
Jahr2 = Range("A10").Value
' Monatsintervall
Range("A10").FormulaR1C1 = "=MID(SAPGetDimensionEffectiveFilter(""DS_1"", ""0CALMONTH""),1,100)"
If InStr(1, Range("A10").Value, Jahr1) Then Jahr1Found = True
If InStr(1, Range("A10").Value, Jahr2) Then Jahr2Found = True
Set MySheet = Excel.Sheets(2)
MySheet.Range("A10").Value = ""
' Auffrischen aus Analysis deaktivieren
iresult = Application.Run("SAPSetRefreshBehaviour", "Off")
' Spalte vergrößern
Columns(4).ColumnWidth = 15 --> Funktioniert nicht bei Aufruf aus Steuerdatei
' Nur ein Jahr gefunden, dann immer Jahr1 ausführen
If Jahr1Found = False Then
If Jahr2Found = True Then
Jahr1 = Jahr2
Jahr1Found = True
Jahr2Found = False
End If
End If
If Jahr1Found = True Then
' Zwei Zeilen einfügen
Rows(10).EntireRow.Insert --> Funktioniert nicht bei Aufruf aus Steuerdatei
Rows(10).EntireRow.Insert --> Funktioniert nicht bei Aufruf aus Steuerdatei
Range("D10").Formula = "'" & Jahr1
' Formeln einfügen
Jahr1 = "*" & Jahr1
Formel = " "
Formel = "=SUMIF(C[-5]," & Chr(34) & Jahr1 & Chr(34) & ",C)"
Range("J10").FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-6]," & Chr(34) & Jahr1 & Chr(34) & ",C)"
Range("K10").FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-7]," & Chr(34) & Jahr1 & Chr(34) & ",C)"
Range("L10").FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-8]," & Chr(34) & Jahr1 & Chr(34) & ",C)"
Range("M10").FormulaR1C1 = Formel
' Formatierung
Range("D10:M10").Select
Selection.Style = "Summe"
Range("D11:I11").Select
Selection.Style = "SAPMemberCell"
Range("J11:M11").Select
Selection.Style = "SAPDataCell"
End If
If Jahr2Found = True Then
'ersten Eintrag für neues Jahr finden
SelectJahr = "JAN" & " " & Jahr2
Set StrAdressR1C1 = Range("E:E").Find(SelectJahr)
If Not StrAdressR1C1 Is Nothing Then
Celle = StrAdressR1C1.Row
' Zwei Zeilen einfügen
Rows(StrAdressR1C1.Row).EntireRow.Insert
Rows(StrAdressR1C1.Row).EntireRow.Insert
'Jahr einfügen
Range(Cells(Celle, 4), Cells(Celle, 4)).Formula = "'" & Jahr2
' Formeln einfügen
Jahr2 = "*" & Jahr2
Formel = " "
Formel = "=SUMIF(C[-5]," & Chr(34) & Jahr2 & Chr(34) & ",C)"
Range(Cells(Celle, 10), Cells(Celle, 10)).FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-6]," & Chr(34) & Jahr2 & Chr(34) & ",C)"
Range(Cells(Celle, 11), Cells(Celle, 11)).FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-7]," & Chr(34) & Jahr2 & Chr(34) & ",C)"
Range(Cells(Celle, 12), Cells(Celle, 12)).FormulaR1C1 = Formel
Formel = " "
Formel = "=SUMIF(C[-8]," & Chr(34) & Jahr2 & Chr(34) & ",C)"
Range(Cells(Celle, 13), Cells(Celle, 13)).FormulaR1C1 = Formel
' Formatierung
Range(Cells(Celle, 4), Cells(Celle, 13)).Select
Selection.Style = "Summe"
End If
End If
Range("A1").Select
End Sub