Сводная таблица из 51 счета


'Option Explicit
''Возможные варианты: ДебетН, КредитН, ДебетО,  КредитО, ДебетК,  КредитК



Sub Svodtabfrom51()
'   Удаление существующего листа сводной таблицы
Dim sName As String
'    sName = "АгрБаланс"
'Call delSheet(sName)

Dim arr0() As String
Dim arr() As String
Dim arr1() As String
Dim arr2() As String
Dim arr3() As String
Dim arr4() As String

Dim arr5() As String 'Контрагенты

ReDim arr0(1)       'П-----
arr0(0) = "60.01"


ReDim arr(1)       '-----------
arr(0) = "60.01"

ReDim arr1(6)       'Займы
arr1(0) = "50.01"
arr1(1) = "66.03"
arr1(2) = "66.04"
arr1(3) = "67.03"
arr1(4) = "67.04"
arr1(5) = "58.03"





Interval = 4


'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", 1) + Interval, "SvodT1", "СуммаД", "СчетК", arr0, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Приход по всем счетам"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT2", "СуммаД", "СчетК", arr, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Чистый приход"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval + 1, "SvodT9", "СуммаД", "Дата", arr, "Банк")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Чистые приходы по банкам"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT3", "СуммаД", "СчетК", arr1, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Займы"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT4", "СуммаД", "СчетК", arr4, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Кредиты"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT5", "СуммаД", "СчетК", arr2, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Возвраты"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT6", "СуммаД", "СчетК", arr3, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Переводы собственных средств"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT7", "СуммаД", "Контрагент", arr5, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаД", 1) - 1) = "Контрагент (приходы)"
'Call fill(Geti22(Sname, "Сумма по полю СуммаД", 1))
'
'Call UPT("Общая ОСВ", Sname, "B5", "A" & Geti22(Sname, "Общий итог", i) + Interval, "SvodT8", "СуммаК", "Контрагент", arr5, "Дата")
'Range("A" & Geti22(Sname, "Сумма по полю СуммаК", 1) - 1) = "Контрагент (расходы)"
'Call fill(Geti22(Sname, "Сумма по полю СуммаК", 1))

NewSheet "60-01"
sName = "60-01"
Call UPT("Общая ОСВ", sName, "B5", "A" & Geti22(sName, "Общий итог", I) + Interval, "SvodT8000", "ДебетК", "Контрагенты", arr0, "Дата")
Range("A" & Geti22(sName, "Сумма по полю ДебетК", 1) - 1) = "Задолженность перед поставщиками 60,01 кред."
Call fill(Geti22(sName, "Сумма по полю ДебетК", 1))
strPoleForSort = "ДебетК"

   LastColumn = Getj(ActiveSheet, 2, "Общий итог") - 1 + 5 'Для фильтрации по "Общий итог" нужно поставит 1
                ActiveSheet.PivotTables("SvodT8000").PivotFields("Контрагенты").AutoSort _
                xlDescending, "Сумма по полю ДебетК", ActiveSheet.PivotTables("SvodT8000"). _
                PivotColumnAxis.PivotLines(4), 1


'NewSheet "60-02"
'sName = "60-02"
'Call UPT("Общая ОСВ", sName, "B5", "A" & Geti22(sName, "Общий итог", I) + Interval, "SvodT9000", "КредитК", "Контрагенты", arr0, "Дата")
'Range("A" & Geti22(sName, "Сумма по полю КредитК", 1) - 1) = "Задолженность перед поставщиками 60,01 кред."
''Call fill(Geti22(sName, "Сумма по полю ДебетК", 1))


'NewSheet "60-02"
'sName = "60-01"
'Call UPT("Общая ОСВ", sName, "B5", "A" & Geti22(sName, "Общий итог", I) + Interval, "SvodT9000", "ДебетК", "Контрагенты", arr, "Дата")
'Range("A" & Geti22(sName, "Сумма по полю ДебетК", 1) - 1) = "Задолженность перед прочими кредиторами 60,02 кред."
'Call fill(Geti22(sName, "Сумма по полю ДебетК", 1))

End Sub
  
Sub UPT(SourseList As String, _
        PVTList As String, _
        FirstRangeSL As String, _
        SecondRange As String, _
        TableName1 As String, _
        SummaPo As String, _
        SortBy As String, _
        ByRef arr() As String, _
        SortByData As String)
 
 
Dim PTcache As PivotCache
    Dim pt As PivotTable
    Dim PItem As PivotItem
    Dim sName As String
 
    sName = PVTList '"Общая ОСВ"
 
    Application.ScreenUpdating = False
 
'   Создание кеэша сводной таблицы
     ActiveWorkbook.PivotCaches.Create( _
      SourceType:=xlDatabase, _
      SourceData:=ThisWorkbook.Sheets(SourseList).Range(FirstRangeSL).CurrentRegion.Address, _
      Version:=xlPivotTableVersion10).CreatePivotTable _
        TableDestination:=ThisWorkbook.Sheets(sName).Range(SecondRange), TableName:=TableName1, _
        DefaultVersion:=xlPivotTableVersion10
 
 
ActiveSheet.PivotTables(TableName1).AddDataField ActiveSheet.PivotTables( _
        TableName1).PivotFields(SummaPo), "Сумма по полю " & SummaPo, xlSum

'=============================================================
'    ActiveSheet.PivotTables(TableName1).PivotFields(SummaPo).CurrentPage = _
'        "(All)"
'    With ActiveSheet.PivotTables(TableName1).PivotFields(SummaPo)
'        .PivotItems("0 ").Visible = False
'    End With
'=============================================================

Set pt = ActiveSheet.PivotTables(TableName1)

    With pt
    If SortByData = "Дата" Then   'ТУТ ЧТО_ТО МЕНЯЛ
       .PivotFields("ОСВ.00").Orientation = xlPageField
       .PivotFields(SummaPo).Orientation = xlPageField
       .PivotFields(SummaPo).PivotItems("0 ").Visible = False
    End If
        'Добавление полей
        .PivotFields(SortBy).Orientation = xlRowField
        .PivotFields(SortByData).Orientation = xlColumnField
        .DataBodyRange.NumberFormat = "#,##0_ ;[Red]-#,##0 "
        .DataBodyRange.ColumnWidth = 13
        .DataBodyRange.Sort
    End With
    
'Сортировка по убыванию

I = Geti2(sName, "Общий итог")
j = Getj2(sName, "Общий итог")

If SortBy = "Контрагенты" Then
    ActiveSheet.PivotTables(TableName1).PivotFields("Контрагенты").AutoSort _
        xlDescending, "Сумма по полю " & SummaPo, ActiveSheet.PivotTables(TableName1). _
        PivotColumnAxis.PivotLines(j - 1), 1

'Exit Sub
End If



If SortBy = "Дата" Then

'----------------------
On Error GoTo ErrorHandler0

With ActiveSheet.PivotTables(TableName1).PivotFields("ОСВ.00") 'pt.PageFields("ОСВ.00")
  For Each PItem In .PivotItems
  Debug.Print PItem.Name
     For i0 = 0 To UBound(arr, 1) - 1
        PItem.Visible = Falce
        If PItem.Name Like arr(i0) Then
          PItem.Visible = True
          Exit For
        End If
     Next i0
   Next
End With

ActiveSheet.PivotTables("SvodT9").PivotFields("Дата").AutoSort xlAscending, _
        "Дата"

GoTo Ends0:
ErrorHandler0:
PItem.Visible = True
Ends0:
'----------------------

Exit Sub
End If



On Error GoTo ErrorHandler

With ActiveSheet.PivotTables(TableName1).PivotFields("ОСВ.00") 'pt.PageFields("ОСВ.00")
  For Each PItem In .PivotItems
'  Debug.Print PItem.Name
     For i0 = 0 To UBound(arr, 1) - 1
        PItem.Visible = Falce
        If PItem.Name Like arr(i0) Then
          PItem.Visible = True
          Exit For
        End If
     Next i0
   Next
End With


GoTo Ends:
ErrorHandler:
'    S1 = Geti22(Sname, "Сумма по полю СуммаД", 1) - 2
'    S2 = Geti22(Sname, "Общий итог", i)
'    Range(S1 & ":" & S2).Delete
PItem.Visible = True
Ends:

End Sub
 
 Function GetERR(TableName1 As String) As String
          GetERR = TableName1
 End Function
 

Function Geti22(SheetName As String, KeyName As String, ByVal starti As Integer) As Integer
  On Error Resume Next
  Dim t As Boolean
  t = False
  Ei = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Row
  ej = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Column
  For I = starti To Ei
    For j = 1 To ej
    If LCase(Trim(ThisWorkbook.Sheets(SheetName).Cells(I, j).Value)) = LCase(KeyName) And t = False Then
       t = True
    End If
      If LCase(Trim(ThisWorkbook.Sheets(SheetName).Cells(I, j).Value)) = LCase(KeyName) And t = True Then
         Geti22 = I
         t = False
      End If
    Next j
  Next I
End Function


Sub fill(I As Integer)
Range(I - 1 & ":" & I - 1).Font.Bold = True
    With Range(I - 1 & ":" & I - 1).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = 0.799981688894314
        .PatternTintAndShade = 0
    End With
End Sub


Sub delSheet(sName As String)
    Application.ScreenUpdating = False
'   Удаление существующего листа сводной таблицы
    On Error Resume Next
    Application.DisplayAlerts = False
   ThisWorkbook.Sheets(sName).Delete
    On Error GoTo 0
    Worksheets.Add
    ActiveSheet.Name = sName
    ActiveWindow.DisplayGridlines = False
 ThisWorkbook.Sheets(sName).Select
End Sub


Sub CheckForFile()
Dim FileName As String
Dim x As Workbook
FileName = "BUDGET.XLSX"
On Error Resume Next
Set x = Workbooks(FileName)
If Err = 0 Then
MsgBox FileName & " is open."
Else
MsgBox FileName & "is not open."
End If
On Error GoTo 0
End Sub
Function Geti2(SheetName As String, Optional KeyName As String) As Integer
  On Error Resume Next
  Dim t As Boolean
  t = True
  If Len(KeyName) = 0 Then
     KeyName = "Код"
  End If
  
  Ei = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Row
  ej = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Column
  
  For I = 1 To Ei
    For j = 1 To ej
      If LCase(Trim(ThisWorkbook.Sheets(SheetName).Cells(I, j).Value)) = LCase(KeyName) And t = True Then
         Geti2 = I
         t = False
      End If
    Next j
  Next I
End Function
Function Getj2(SheetName As String, Optional KeyName As String) As Integer
  On Error Resume Next
  Dim t As Boolean
  t = True
  If Len(KeyName) = 0 Then
     KeyName = "Код"
  End If
  
  Ei = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Row
  ej = ThisWorkbook.Sheets(SheetName).Cells.SpecialCells(xlLastCell).Column
  
  For I = 1 To Ei
    For j = 1 To ej
      If LCase(Trim(ThisWorkbook.Sheets(SheetName).Cells(I, j).Value)) = LCase(KeyName) And t = True Then
         Getj2 = j
         t = False
      End If
    Next j
  Next I
End Function



Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *