Advertisement
AlanElston

For Yasser

Dec 5th, 2018
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit ' http://www.eileenslounge.com/viewtopic.php?f=30&t=31362&p=242769#p242769
  2. Sub Test()
  3.     Dim wsSalary    As Worksheet
  4.     Dim wsTB        As Worksheet
  5.     Dim a           As Variant
  6.     Dim b           As Variant
  7.     Dim aFT         As Variant
  8.     Dim aSD         As Variant
  9.     Dim x           As Double
  10.     Dim m           As Long
  11.     Dim i           As Long
  12.     Dim j           As Long
  13.     Dim n           As Long
  14.     Dim c           As Long
  15.     Dim r           As Long
  16.     Dim d           As Long
  17.         Set wsSalary = ThisWorkbook.Worksheets("Salary")
  18.         Set wsTB = ThisWorkbook.Worksheets("TB")
  19.         With wsTB.Range("A6:AI10000")
  20.             .ClearContents: .Borders.Value = 0: .Cells.UnMerge: .Interior.Color = xlNone
  21.         End With
  22.         m = wsSalary.Cells(Rows.Count, 1).End(xlUp).Row - 2
  23.         If m = 1 Then Exit Sub
  24.         a = wsSalary.Range("A2:CM" & m).Value
  25.         ReDim b(1 To 2 * (UBound(a, 1) + 3 * Application.RoundUp(UBound(a, 1) / 15, 0)), 1 To 35)
  26.  
  27.         aFT = Array(1, 16, 17, 18, 19, 20, 21, 22, , 38, 39, 41, 40, 42, 43, 45, 44, 29, 30, 34, 32, 33, 35, 48, 49, 50, 51, , 54, 53, 55, 91, 2, 8, 14)
  28.         aSD = Array(, 56, 57, 58, 60, 61, , 63, 64, 66, , 59, 64, 65, 67, 70, 68, 69, 71, 79, 78, 81, 82, 83, 74, 85, 86, 87, 90)
  29.  
  30.         For i = LBound(a, 1) To UBound(a, 1)
  31.             n = n + 1
  32.             For j = 1 To UBound(b, 2)
  33.                 If Not IsMissing(aFT(j - 1)) Then b(n, j) = a(i, aFT(j - 1))
  34.             Next j
  35.             b(n, 9) = Val(a(i, 26)) + Val(a(i, 27))
  36.             b(n, 28) = Val(a(i, 49)) + Val(a(i, 50)) + Val(a(i, 51))
  37.  
  38.             n = n + 1
  39.             For j = 1 To UBound(b, 2)
  40.                 If j = 30 Then Exit For
  41.                 If Not IsMissing(aSD(j - 1)) Then b(n, j) = a(i, aSD(j - 1))
  42.             Next j
  43.             b(n, 7) = Val(b(n, 2)) + Val(b(n, 3)) + Val(b(n, 4)) + Val(b(n, 5)) + Val(b(n, 6))
  44.             b(n, 11) = Val(b(n, 8)) + Val(b(n, 9)) + Val(b(n, 10))
  45.         Next i
  46.  
  47.         wsTB.Range("A6").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  48. Rem 2 Insert 3 empty rows
  49. Dim NxtRw As Range ' The next data row
  50. Set NxtRw = wsTB.Range("B6")
  51.     Do While NxtRw.Value <> "" ' Keep going while the next data row is not empty
  52.     wsTB.Rows("" & NxtRw.Row + 30 & ":" & NxtRw.Row + 32 & "").Insert Shift:=xlDown
  53.      Set NxtRw = wsTB.Range("B" & NxtRw.Row + 33 & "")
  54.     Loop '  While NxtRw.Value <> ""
  55. Rem 3 Get an array of row indicies to identify the first and last rows of data sections
  56. Dim Lr As Long: Let Lr = wsTB.Range("B" & Rows.Count & "").End(xlUp).Row
  57. Dim Cnt As Long
  58. Dim strHindiSees As String: Let strHindiSees = "6 " ' Assume the first row indicie is 6
  59. Dim NextHindiSee As Long: Let NextHindiSee = 6
  60.     For Cnt = 1 To 100000
  61.      Let NextHindiSee = NextHindiSee + 29 ' The next indicie will be 29 rows down
  62.        If NextHindiSee >= Lr Then ' This is to check if we are at or over the final data row
  63.         Let NextHindiSee = Lr
  64.          Let strHindiSees = strHindiSees & NextHindiSee & " " ' add indicie for last data row in current data section
  65.         Exit For
  66.         Else
  67.          Let strHindiSees = strHindiSees & NextHindiSee & " " ' add indicie for last data row in current data section
  68.         Let NextHindiSee = NextHindiSee + 4 ' The next row indicie for the start of next data section
  69.         Let strHindiSees = strHindiSees & NextHindiSee & " " ' add indicie for first data row in next data section
  70.        End If
  71.     Next Cnt
  72.  Let strHindiSees = Mid(strHindiSees, 1, Len(strHindiSees) - 1) ' Take off last space
  73. Dim arrHindiSees() As String ' Split below, returns string type elements
  74. Let arrHindiSees() = Split(strHindiSees, " ", -1, vbBinaryCompare) ' Split returns a 1 dimensional array of the split up strHindiSees using a " " as the seperator: It chops strHindiSees  up at the " "
  75. Rem 4 ' see http://www.eileenslounge.com/viewtopic.php?f=30&t=31362&p=242769#p242761
  76.    For Cnt = 0 To UBound(arrHindiSees()) Step 2
  77.     Dim MeFukyFormula As String
  78.     ' First Formula row
  79.     Let MeFukyFormula = FuncyFormula(arrHindiSees(Cnt + 1) - 1, arrHindiSees(Cnt))
  80.      Let wsTB.Range("B" & arrHindiSees(Cnt + 1) + 1 & ":AF" & arrHindiSees(Cnt + 1) + 1 & "").Value = MeFukyFormula
  81.     ' Second Formula row
  82.     Let MeFukyFormula = FuncyFormula(arrHindiSees(Cnt + 1), arrHindiSees(Cnt) + 1)
  83.      Let wsTB.Range("B" & arrHindiSees(Cnt + 1) + 2 & ":AF" & arrHindiSees(Cnt + 1) + 2 & "").Value = MeFukyFormula
  84.     Next Cnt
  85. End Sub
  86. Function FuncyFormula(ByVal BL As Long, ByVal TL As Long) As String
  87. Dim Cnt As Long
  88.     For Cnt = BL To TL Step -2
  89.     Dim MeStrungOut As String
  90.      Let MeStrungOut = ",B" & Cnt & MeStrungOut
  91.     Next Cnt
  92. Let MeStrungOut = Replace(MeStrungOut, ",", "", 1, 1, vbBinaryCompare) 'take off just 1 ","
  93. Let FuncyFormula = "=sum(" & MeStrungOut & ")"
  94. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement