Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Sub produce_sum()
  2.     Dim arrValues() As Variant
  3.     Dim TotalRows As Long
  4.     Dim i As Long
  5.     Dim j As Long
  6.     Dim Report As Worksheet 'Set up your new worksheet variable.
  7.    Set Report = Excel.ActiveSheet 'Assign the active sheet to the variable.
  8.    lRow = Range("D" & Rows.Count).End(xlUp).Row
  9.     Set locations = Range("D2:D" & lRow)
  10.     Range("G1").Value = "SUM"
  11.     TotalRows = Rows(Rows.Count).End(xlUp).Row
  12.     ReDim arrValues(1 To TotalRows)
  13.    
  14.     For i = 1 To TotalRows Step 3
  15.         If locations(i).Value <> locations(i + 1).Value Or locations(i + 1).Value <> locations(i + 2).Value Or locations(i + 2).Value <> locations(i).Value Then
  16.             locations(i).Interior.ColorIndex = 3
  17.             locations(i + 1).Interior.ColorIndex = 3
  18.             locations(i + 2).Interior.ColorIndex = 3
  19.         End If
  20.     Next
  21.    
  22.     For i = 2 To TotalRows
  23.         arrValues(i) = Round(Cells(i, 3).Value, 3)
  24.         Exit For
  25.     Next
  26.    
  27.     j = 2
  28.     For i = 2 To TotalRows - 1
  29.         If i = j Or i = j + 3 Then
  30.             ActiveSheet.Cells(i, 7).Value = Application.Sum(Range(Cells(i, 3), Cells(i + 2, 3)))
  31.             j = i
  32.         End If
  33.     Next
  34. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement