Sub Sortiere
ActiveCell.CurrentRegion.Sort _
Key1:=Range("I2"), Order1:=xlAscending, _
Key2:=Range("G2"), Order2:=xlAscending, _
Key3:=Range("A2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal
Selection.Subtotal GroupBy:=10, Function:=xlSum, TotalList:=Array(4, 11, 12 _
, 13, 14, 15), Replace:=True
End Sub
Sub Sortiere()
Dim Titel As Range, arrSpalten() As Long, arrNamen, intI, intZeilen, Zelle As Range
arrNamen = Array("Feld01", "Feld03", "Feld07", "Feld08", "Feld13", "Feld12")
Set Titel = ActiveCell.CurrentRegion.Rows(1)
For intI = LBound(arrNamen) To UBound(arrNamen)
Set Zelle = Titel.Find(what:=arrNamen(intI), Lookat:=xlWhole, LookIn:=xlValues)
If Not Zelle Is Nothing Then
intZeilen = intZeilen + 1
ReDim Preserve arrSpalten(1 To intZeilen)
arrSpalten(intZeilen) = Zelle.Column
End If
Next
ActiveCell.CurrentRegion.Sort _
Key1:=Range("I2"), Order1:=xlAscending, _
Key2:=Range("G2"), Order2:=xlAscending, _
Key3:=Range("A2"), Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
DataOption3:=xlSortNormal
If intZeilen > 0 Then
Selection.Subtotal GroupBy:=10, Function:=xlSum, TotalList:=arrSpalten, _
Replace:=True
End If
End Sub
Sub TotalList_dynamisch()'Quelle: http://www.xtremevbtalk.com/archive/index.php/t-261411.html
Dim aryTotalList()
Dim intColMax As Integer, intColNum As Integer, intColGroup As Integer, i As Integer
intColMax = 0
intColMax = Cells(1).CurrentRegion.Columns.Count 'Spaltenanzahl Insgesamt?
intColNum = 4 'ab wann soll summiert werden?
intColGroup = 9 'Welches Kriterium soll analysiert werden
i = 0
Do Until intColNum > intColMax
'aber die Spalten 5 bis 11 ignorieren mit dem "verpönten" Goto
If intColNum > 4 And intColNum < 12 Then GoTo Lbl_SkipColumn
i = i + 1
ReDim Preserve aryTotalList(1 To i)
aryTotalList(i) = intColNum 'Array TotalList dynamisch bilden
Lbl_SkipColumn:
intColNum = intColNum + 1
Loop
Cells(1, 1).Select
Selection.Subtotal GroupBy:=intColGroup, Function:=xlSum, TotalList:=aryTotalList, _
Replace:=True, PageBreaks:=False, SummaryBelowData:=True
End Sub
'If-Anweisung schliessen
Do Until intColNum > intColMax
If intColNum > 4 And intColNum < 12 Then
i = i + 1
ReDim Preserve aryTotalList(1 To i)
aryTotalList(i) = intColNum 'Array TotalList dynamisch bilden
End If
intColNum = intColNum + 1
Loop
'oder mit der hier noch flexibler einsetzbaren Select Case Anweisung
Do Until intColNum > intColMax
Select Case intColNum
Case 4 To 12
i = i + 1
ReDim Preserve aryTotalList(1 To i)
aryTotalList(i) = intColNum 'Array TotalList dynamisch bilden
Case Else
'do nothing
End Select
intColNum = intColNum + 1
Loop