Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. Option Explicit
  2.  
  3.  
  4. Sub makeSummary()
  5.  
  6. Dim inws As Worksheet
  7. Dim outws As Worksheet
  8.  
  9. Dim fndRange As Range
  10. Dim zell As Range
  11.  
  12. Dim firstRow As Integer
  13. Dim lastRow As Integer
  14. Dim colctr As Integer
  15. Dim totalCol As Integer
  16. Dim LastCol As Integer
  17.  
  18. Dim ctr As Long
  19. Dim arrBound As Long
  20.  
  21. Dim distVals() As String
  22. Dim newRows() As String
  23.  
  24.  
  25. ' Initialize variables
  26. Set inws = Sheets("Items")
  27. Set outws = Sheets("Summary_Sheet")
  28. outws.Cells.Clear
  29. Set fndRange = Range(GetLast(3, inws.Cells))
  30. LastCol = fndRange.Column
  31. lastRow = fndRange.Row
  32. Set fndRange = Nothing
  33.  
  34. 'populate the header columns in the output worksheet.
  35. For ctr = 1 To LastCol
  36. outws.Cells(1, ctr) = inws.Cells(1, ctr).Value
  37. Next ctr
  38.  
  39. ' redim array, and populate with unique SFC values
  40. ReDim distVals(1)
  41. distVals(1) = inws.Cells(2, 1)
  42. For ctr = 2 To lastRow
  43. If inws.Cells(ctr, 1).Value <> distVals(UBound(distVals)) Then
  44. ReDim Preserve distVals(UBound(distVals) + 1)
  45. distVals(UBound(distVals)) = inws.Cells(ctr, 1).Value
  46. End If
  47. Next ctr
  48.  
  49. 'Get upper bound of search array and use it to set max row value of newrows array
  50. arrBound = UBound(distVals)
  51. ReDim newRows(1 To arrBound, 1 To LastCol)
  52.  
  53. 'build array,
  54. For ctr = 1 To arrBound
  55. Set fndRange = inws.Cells.Find(distVals(ctr), lookat:=xlPart, LookIn:=xlFormulas)
  56. firstRow = fndRange.Row
  57. lastRow = fndRange.Row
  58. Do Until inws.Cells(lastRow + 1, 1) <> distVals(ctr)
  59. lastRow = lastRow + 1
  60. Loop
  61. 'fill row
  62. For colctr = 1 To LastCol
  63. newRows(ctr, colctr) = inws.Cells(firstRow, colctr)
  64. Next colctr
  65. 'Get total of totals in SFC
  66. newRows(ctr, 5) = WorksheetFunction.Sum(Range(inws.Cells(firstRow, 5), inws.Cells(lastRow, 5)))
  67. Next ctr
  68.  
  69. 'clean up of destination sheet
  70. With outws
  71. .Columns("E").NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
  72. .Range(.Cells(2, 1).Address, .Cells(arrBound + 1, LastCol).Address) = newRows
  73. 'excel doesn't recognize the numbers as numbers unless you multiply by 1 and drop the value back down.
  74. For Each zell In .Range(.Cells(2, 5).Address, .Cells(arrBound + 1, 5).Address)
  75. zell.Value = zell.Value * 1
  76. Next zell
  77. .Calculate
  78. End With
  79.  
  80.  
  81.  
  82.  
  83. End Sub
  84.  
  85. Function GetLast(choice As Long, rng As Range)
  86. ' 1 = GetLast row
  87. ' 2 = GetLast column
  88. ' 3 = GetLast cell
  89.  
  90. Dim ReturnRng As Range
  91.  
  92. Set ReturnRng = rng.Find(What:="*", After:=rng.Cells(1), lookat:=xlPart, LookIn:=xlFormulas, _
  93. SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False)
  94.  
  95. If Not ReturnRng Is Nothing Then
  96. With ReturnRng
  97. Select Case choice
  98. Case 1
  99. GetLast = .Row
  100. Case 2
  101. GetLast = .Column
  102. Case 3
  103. GetLast = .Address
  104. Case Else
  105. End Select
  106. End With
  107. End If
  108. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement