Pivot Tabellen Erstellen in Excel

  • VB.NET

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von minimaster.

    Pivot Tabellen Erstellen in Excel

    Guten Tag,

    ich finde im Internet keine vernünftigen Referenzen zum Thema Pivot über VB.Net.
    Ich will über VB.Net eine automatisierte Pivot Tabelle erstellen lassen. Diese habe ich vorerst über ein
    Makro in Excel erledigt, das war auch soweit kein Problem nur leider kann man das Makro nicht so einfach in VB.net übernehmen...

    VB.NET-Quellcode

    1. Sub pivot()
    2. Dim Spaltenlaenge As Byte
    3. Dim Zeilenlaenge As Byte
    4. Spaltenlaenge = 2
    5. Do Until xlApp.Range("A" & Spaltenlaenge).Value = ""
    6. Spaltenlaenge = Spaltenlaenge + 1
    7. Loop
    8. Spaltenlaenge = Spaltenlaenge - 1
    9. Zeilenlaenge = 1
    10. Do Until xlApp.Cells(1, Zeilenlaenge).Value = ""
    11. Zeilenlaenge = Zeilenlaenge + 1
    12. Loop
    13. Zeilenlaenge = Zeilenlaenge - 1
    14. xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(Spaltenlaenge, Zeilenlaenge)).Select()
    15. xlApp.Cells(Spaltenlaenge, Zeilenlaenge).Activate()
    16. '''''''''''''' HIER FANGEN DIE PROBLEME AN '''''''''''''''''''
    17. xlMappe.PivotCaches.Add(SourceType:=xlZelle.xlDatabase, SourceData:= _
    18. "Tabelle2!R1C1:R26C9").CreatePivotTable(TableDestination:="", TableName:= _
    19. "PivotTable2")
    20. xlBlatt.PivotTableWizard(TableDestination:=xlBlatt.Cells(3, 1))
    21. xlBlatt.Cells(3, 1).Select()
    22. xlBlatt.PivotTables("PivotTable2").SmallGrid = False
    23. xlBlatt.PivotTables("PivotTable2").AddFields(RowFields:=xlBlatt.Array( _
    24. "Artikel Lot", "Lot Nr", "Farbe Lot"), ColumnFields:="Größe")
    25. xlBlatt.PivotTables("PivotTable2").PivotFields("Bestand").Orientation = _
    26. xlBlatt.xlDataField
    27. ExcelClose()
    28. End Sub


    Wie man sieht selektiert der erstmal den nötigen Bereich, aber ab der makierten Zeile funktioniert gar nichts mehr...
    und da ich leider sonst nichts im Internet finde wollte ich dies bezüglich euch mal um Hilfe bitten:)
    I found it helpful to develop the code in VBA and then I just port it to VB.Net.

    To have good code portability I'm declaring your "xlApp" object in my VB.NEt code as "Application". This way the VB.Net code should run fine in VBA too and vice versa. A bit problematic is that the data type "variant" does not exist in VB.net. Sometimes the variant data type (and especially variant arrays) are very handy and therefore I deliberatly stick to VBA in these cases.

    VB.NET-Quellcode

    1. ' the name "Application" is chosen to have code copy and paste compatibility with the VBA editor
    2. Public WithEvents Application As Microsoft.Office.Interop.Excel.Application


    Specifically in your code this "SourceType:=xlZelle.xlDatabase" looks a bit strange to me.

    Furthermore I would first create a named range for the Pivottable source and then use the named range for Sourcedata.

    The line with PivotTableWizard should not be required at all to create a pivottable via code. I think you can just delete that line without effect.

    Ich merke gerade das ich fälschlicherweise in English geantwortet habe. Ich hoffe es ist trotzdem einigermassen verständlich.

    Noch mal etwas Zusatzcode wie es aussehen könnte - Alles ungetestet

    VB.NET-Quellcode

    1. #Region "Imports"
    2. ' Namespace Imports
    3. Imports Microsoft.Office.Interop.Excel
    4. ' Enum imports
    5. Imports Microsoft.Office.Interop.Excel.Constants
    6. Imports Microsoft.Office.Interop.Excel.XlYesNoGuess
    7. Imports Microsoft.Office.Interop.Excel.XlSortDataOption
    8. Imports Microsoft.Office.Interop.Excel.XlSortOrder
    9. Imports Microsoft.Office.Interop.Excel.XlSortType
    10. Imports Microsoft.Office.Interop.Excel.XlConsolidationFunction
    11. Imports Microsoft.Office.Interop.Excel.XlFindLookIn
    12. Imports Microsoft.Office.Interop.Excel.XlLookAt
    13. Imports Microsoft.Office.Interop.Excel.XlSearchOrder
    14. Imports Microsoft.Office.Interop.Excel.XlSearchDirection
    15. Imports Microsoft.Office.Interop.Excel.XlPasteType
    16. Imports Microsoft.Office.Interop.Excel.XlPattern
    17. Imports Microsoft.Office.Interop.Excel.XlColorIndex
    18. Imports Microsoft.Office.Interop.Excel.XlLineStyle
    19. Imports Microsoft.Office.Interop.Excel.XlBordersIndex
    20. Imports Microsoft.Office.Interop.Excel.XlBorderWeight
    21. Imports Microsoft.Office.Interop.Excel.XlSheetType
    22. Imports Microsoft.Office.Interop.Excel.XlPivotFieldOrientation
    23. Imports Microsoft.Office.Interop.Excel.XlPivotTableMissingItems
    24. Imports Microsoft.Office.Interop.Excel.XlPTSelectionMode
    25. Imports Microsoft.Office.Interop.Excel.XlPivotFieldCalculation
    26. Imports Microsoft.Office.Interop.Excel.XlLayoutRowType
    27. Imports Microsoft.Office.Interop.Excel.XlSourceType
    28. Imports Microsoft.Office.Interop.Excel.XlPivotTableVersionList
    29. Imports Microsoft.Office.Interop.Excel.XlPivotFilterType
    30. Imports Microsoft.Office.Interop.Excel.XlPivotTableSourceType
    31. #End Region
    32. Module ptTools1
    33. Sub create_named_Range_db_with_autosize()
    34. On Error Resume Next
    35. With Application
    36. .ActiveWorkbook.Names.Item("DBrange").Delete()
    37. ' in VBA this is slightly easier, but won't be accepted in Vb.net
    38. ' .ActiveWorkbook.Names("DBrange").Delete()
    39. With .ActiveSheet
    40. .ActiveWorkbook.Names.Add(Name:="DBrange", RefersToR1C1:="=OFFSET(" _
    41. & .Name & "!R" & .UsedRange.Row & "C" & .UsedRange.Column & ",0,0,COUNTA(" _
    42. & .Name & "!C2)," & .UsedRange.Columns.Count & ")")
    43. End With
    44. End With
    45. End Sub
    46. '------------------------------------------------------------------------
    47. Sub CreatePivotTable_fromNamedRange_db(Optional data As String)
    48. Dim wsPT As Worksheet
    49. Dim pt As PivotTable
    50. Dim MyPeriods(6) As Boolean
    51. With Application
    52. .Sheets.Add()
    53. wsPT = .ActiveSheetwsPT.Name = "Pivot" & .Sheets.Count
    54. .ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="=DBrange", _Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=wsPT.Name & "!R3C1", _
    55. TableName:="PT" & .Sheets.Count, DefaultVersion:=xlPivotTableVersion12)
    56. On Error Resume Next
    57. wsPT.Select()
    58. .Cells(3, 1).Select()pt = .ActiveSheet.PivotTables("PT" & .Sheets.Count)With pt.PivotFields("Date")
    59. .Orientation = xlColumnField
    60. .Position = 1
    61. End With
    62. pt.PivotFields("Date").PivotFilters.Add(Type:=xlDateBetween, Value1:="1/1/2010", Value2:="6/30/2012")
    63. With pt.PivotFields("Country")
    64. .Orientation = xlRowField
    65. .Position = 1
    66. End With
    67. If data <> "" Then
    68. pt.AddDataField(.ActiveSheet.PivotTables("PT" & .Sheets.Count).PivotFields(data), "Sum of " & data, xlSum)
    69. Elsept.AddDataField(.ActiveSheet.PivotTables("PT" & .Sheets.Count).PivotFields("Revenue"), "Sum of Revenue", xlSum)
    70. pt.AddDataField(.ActiveSheet.PivotTables("PT" & .Sheets.Count).PivotFields("USD Revenue"), "Sum of USD Revenue", xlSum)
    71. End If
    72. On Error GoTo 0
    73. With pt
    74. .InGridDropZones = True
    75. .RowAxisLayout(xlTabularRow)
    76. End With
    77. MyPeriods(0) = False
    78. MyPeriods(1) = False
    79. MyPeriods(2) = False
    80. MyPeriods(3) = False
    81. MyPeriods(4) = True
    82. MyPeriods(5) = True
    83. MyPeriods(6) = True
    84. .Range("B3").Group(Start:=True, End:=True, Periods:=MyPeriods)
    85. ' the easier VBA version
    86. '.Range("B3").Group(Start:=True, End:=True, Periods:=Array(False, False, False, False, True, True, True)) '
    87. End With
    88. End Sub
    89. End Module

    Dieser Beitrag wurde bereits 16 mal editiert, zuletzt von „minimaster“ ()