Advertisement
oshkoshbagoshh

Txt_pivotTables

Aug 18th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Macro1_UpdateAndCopy()
  2. Application.DisplayAlerts = False
  3. Dim pt As PivotTable
  4. Set pt = Sheets("Sheet1").PivotTables("PivotTable1")
  5. pt.RefreshTable
  6.  
  7. Sheets("Sheet1").Select
  8. 'Create sheet for results or delete existing and create
  9. Dim sh As Worksheet, flg As Boolean
  10. For Each sh In Worksheets
  11. If sh.Name Like "Matrix" Then flg = True: Exit For
  12. Next
  13.  
  14. If flg = True Then
  15. Sheets("Matrix").Select
  16. ActiveWindow.SelectedSheets.Delete
  17. Sheets.Add.Name = "Matrix"
  18. Else
  19. Sheets.Add.Name = "Matrix"
  20. End If
  21.  
  22. 'Copy Pivot
  23. Sheets("Sheet1").Select
  24. Dim LastRow As Long
  25.     With ActiveSheet
  26.         LastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
  27.     End With
  28.    
  29. Dim LastColumn As Long
  30.     With ActiveSheet
  31.         LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
  32.     End With
  33.    
  34. Range(Cells(1, 1), Cells(LastRow, LastColumn)).Select
  35. Selection.Copy
  36.  
  37. Sheets("Matrix").Select
  38. Cells(1, 1).Select
  39.  
  40. Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  41.         :=False, Transpose:=False
  42.        
  43. Application.DisplayAlerts = True
  44.  
  45. Call Macro2_InsertText
  46. End Sub
  47.  
  48. Sub Macro2_InsertText()
  49.  
  50.  
  51. Dim LastRow As Long
  52.     With ActiveSheet
  53.         LastRow = .Range("B1").SpecialCells(xlCellTypeLastCell).Row
  54.     End With
  55.    
  56. Dim LastColumn As Long
  57.     With ActiveSheet
  58.         LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
  59.     End With
  60.    
  61.     Range(Columns(1), Columns(LastColumn)).Select
  62.     With Selection
  63.         .HorizontalAlignment = xlCenter
  64.         .VerticalAlignment = xlBottom
  65.         .WrapText = False
  66.         .Orientation = 0
  67.         .AddIndent = False
  68.         .IndentLevel = 0
  69.         .ShrinkToFit = False
  70.         .ReadingOrder = xlContext
  71.         .MergeCells = False
  72.     End With
  73.    
  74. Dim a
  75. For irow = 4 To LastRow
  76.     a = Cells(irow, 2).Value
  77.     For icol = 3 To LastColumn
  78.         If (Cells(irow, icol).Value > 0) Then
  79.             Cells(irow, icol).Value = a
  80.         End If
  81.     Next icol
  82. Next irow
  83.    
  84. Columns("B").EntireColumn.Delete
  85. Columns("A:AZ").EntireColumn.AutoFit
  86.    
  87. Call Macro3_FitTable
  88. End Sub
  89.  
  90. Sub Macro3_FitTable()
  91.  
  92.  
  93. Dim LastRow As Long
  94.     With ActiveSheet
  95.         LastRow = .Range("B1").SpecialCells(xlCellTypeLastCell).Row
  96.     End With
  97.    
  98. Dim LastColumn As Long
  99.     With ActiveSheet
  100.         LastColumn = .Range("A1").SpecialCells(xlCellTypeLastCell).Column
  101.     End With
  102.    
  103. Dim b, temp
  104. b = 0
  105. Dim groupRowNum
  106.     For i = 4 To LastRow
  107.         If Cells(i, 1) <> b And Cells(i, 1) > 0 Then
  108.             b = Cells(i, 1)
  109.             groupRowNum = i
  110.             GoTo skipLoop
  111.         End If
  112.         temp = groupRowNum
  113.         For icol = 2 To LastColumn
  114.             If Cells(i, icol) <> 0 Then
  115.                 Do While temp < i
  116.                     If Cells(temp, icol) = 0 Then
  117.                         Cells(temp, icol).Value = Cells(i, icol)
  118.                         Cells(i, icol) = ""
  119.                         temp = i
  120.                         GoTo endLoop
  121.                     Else
  122.                         temp = temp + 1
  123.                     End If
  124. endLoop:
  125.                 Loop
  126.             End If
  127.         Next icol
  128. skipLoop:
  129.     Next i
  130.    
  131.     deletedRows = 0
  132.         For x = LastRow To 1 Step -1
  133.             If Application.WorksheetFunction.CountA(Rows(x)) = 0 Then
  134.             Rows(x).Select
  135.             Selection.Delete Shift:=xlUp
  136.             deletedRows = deletedRows + 1
  137.             End If
  138.         Next x
  139. Call Macro4_MergeAndColor(LastRow - deletedRows)
  140. End Sub
  141.  
  142. Sub Macro4_MergeAndColor(LastRow As Long)
  143. Cells(1, 1).Value = ""
  144.    
  145. Dim LastColumn As Long
  146.     With ActiveSheet
  147.         LastColumn = .Range("A3").SpecialCells(xlCellTypeLastCell).Column
  148.     End With
  149.    
  150.     Range(Cells(4, 1), Cells(LastRow, 1)).Select
  151.    
  152.  With Selection.Font
  153.         .Name = "Calibri"
  154.         .Size = 12
  155.         .Strikethrough = False
  156.         .Superscript = False
  157.         .Subscript = False
  158.         .OutlineFont = False
  159.         .Shadow = False
  160.         .Underline = xlUnderlineStyleNone
  161.         .ThemeColor = xlThemeColorDark1
  162.         .TintAndShade = 0
  163.         .ThemeFont = xlThemeFontMinor
  164.     End With
  165.     With Selection.Interior
  166.         .Pattern = xlSolid
  167.         .PatternColorIndex = xlAutomatic
  168.         .Color = 12611584
  169.         .TintAndShade = 0
  170.         .PatternTintAndShade = 0
  171.     End With
  172.    
  173.     Range(Cells(2, 2), Cells(3, LastColumn - 1)).Select
  174.    
  175.     With Selection.Interior
  176.         .Pattern = xlSolid
  177.         .PatternColorIndex = xlAutomatic
  178.         .ThemeColor = xlThemeColorDark2
  179.         .TintAndShade = -9.99786370433668E-02
  180.         .PatternTintAndShade = 0
  181.     End With
  182.    
  183.     Range(Cells(2, 1), Cells(LastRow, LastColumn - 1)).Select
  184.    
  185.     With Selection.Borders(xlInsideHorizontal)
  186.         .LineStyle = xlContinuous
  187.         .ColorIndex = 0
  188.         .TintAndShade = 0
  189.         .Weight = xlThin
  190.     End With
  191.    
  192.     'merge Groups
  193.    vallindex = 4
  194.     For ind = 5 To LastRow
  195.         If Cells(ind, 1) < 1 Then
  196.             Range(Cells(vallindex, 1), Cells(ind, 1)).Select
  197.             Selection.Merge
  198.             With Selection
  199.                 .HorizontalAlignment = xlCenter
  200.                 .VerticalAlignment = xlTop
  201.                 .WrapText = False
  202.                 .Orientation = 0
  203.                 .AddIndent = False
  204.                 .IndentLevel = 0
  205.                 .ShrinkToFit = False
  206.                 .ReadingOrder = xlContext
  207.                 .MergeCells = True
  208.             End With
  209.         End If
  210.    
  211.     vallindex = ind
  212.     Next ind
  213.    
  214.     'merge Family
  215.    vallindex = 2
  216.     For ind = 3 To LastColumn - 1
  217.         If Cells(2, ind) < 1 Then
  218.             Range(Cells(2, vallindex), Cells(2, ind)).Select
  219.             Selection.Merge
  220.             With Selection
  221.                 .HorizontalAlignment = xlCenter
  222.                 .VerticalAlignment = xlTop
  223.                 .WrapText = False
  224.                 .Orientation = 0
  225.                 .AddIndent = False
  226.                 .IndentLevel = 0
  227.                 .ShrinkToFit = False
  228.                 .ReadingOrder = xlContext
  229.                 .MergeCells = True
  230.             End With
  231.         End If
  232.    
  233.     vallindex = ind
  234.     Next ind
  235.    
  236.    
  237.     Range(Cells(1, 1), Cells(LastRow, LastColumn - 1)).Select
  238.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  239.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  240.     With Selection.Borders(xlEdgeLeft)
  241.         .LineStyle = xlContinuous
  242.         .ColorIndex = 0
  243.         .TintAndShade = 0
  244.         .Weight = xlThin
  245.     End With
  246.     With Selection.Borders(xlEdgeTop)
  247.         .LineStyle = xlContinuous
  248.         .ColorIndex = 0
  249.         .TintAndShade = 0
  250.         .Weight = xlThin
  251.     End With
  252.     With Selection.Borders(xlEdgeBottom)
  253.         .LineStyle = xlContinuous
  254.         .ColorIndex = 0
  255.         .TintAndShade = 0
  256.         .Weight = xlThin
  257.     End With
  258.     With Selection.Borders(xlEdgeRight)
  259.         .LineStyle = xlContinuous
  260.         .ColorIndex = 0
  261.         .TintAndShade = 0
  262.         .Weight = xlThin
  263.     End With
  264.     With Selection.Borders(xlInsideVertical)
  265.         .LineStyle = xlContinuous
  266.         .ColorIndex = 0
  267.         .TintAndShade = 0
  268.         .Weight = xlThin
  269.     End With
  270.     With Selection.Borders(xlInsideHorizontal)
  271.         .LineStyle = xlContinuous
  272.         .ColorIndex = 0
  273.         .TintAndShade = 0
  274.         .Weight = xlThin
  275.     End With
  276.    
  277.    
  278.     Cells(1, 1).Select
  279. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement