Trai60

ProcessProlificData

Nov 24th, 2024 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. Sub ProcessProlificData()
  2. Dim wsSource As Worksheet
  3. Dim wsTarget As Worksheet
  4. Dim lastRow As Long
  5. Dim i As Long
  6.  
  7. Application.ScreenUpdating = False
  8.  
  9. ' Get source worksheet
  10. Set wsSource = ActiveSheet
  11.  
  12. ' Create new worksheet
  13. Set wsTarget = ThisWorkbook.Sheets.Add(After:=wsSource)
  14. wsTarget.Name = "Processed_Data"
  15.  
  16. ' Copy original data
  17. wsSource.UsedRange.Copy wsTarget.Range("A1")
  18. lastRow = wsTarget.Cells(wsTarget.Rows.count, "A").End(xlUp).row
  19.  
  20. ' Add new column headers
  21. With wsTarget
  22. .Cells(1, 9).Value = "Duration"
  23. .Cells(1, 10).Value = "Reward (£)"
  24. .Cells(1, 11).Value = "Reward ($)"
  25. .Cells(1, 12).Value = "Bonus (£)"
  26. .Cells(1, 13).Value = "Bonus ($)"
  27. .Cells(1, 14).Value = "Year"
  28. .Cells(1, 15).Value = "Month"
  29. End With
  30.  
  31. ' Process each row
  32. For i = 2 To lastRow
  33. ' Process dates and calculate duration
  34. If Not IsEmpty(wsTarget.Cells(i, 4)) And Not IsEmpty(wsTarget.Cells(i, 5)) Then
  35. wsTarget.Cells(i, 9).Value = wsTarget.Cells(i, 5).Value - wsTarget.Cells(i, 4).Value
  36. End If
  37.  
  38. ' Process Reward
  39. Dim rewardText As String
  40. rewardText = Trim(wsTarget.Cells(i, 2).Value)
  41.  
  42. If Left(rewardText, 1) = "$" Then
  43. wsTarget.Cells(i, 11).Value = CDbl(Mid(rewardText, 2)) ' USD column
  44. wsTarget.Cells(i, 10).Value = "" ' Clear GBP column
  45. ElseIf Left(rewardText, 1) = "£" Then
  46. wsTarget.Cells(i, 10).Value = CDbl(Mid(rewardText, 2)) ' GBP column
  47. wsTarget.Cells(i, 11).Value = "" ' Clear USD column
  48. End If
  49.  
  50. ' Process Bonus
  51. Dim bonusText As String
  52. bonusText = Trim(wsTarget.Cells(i, 3).Value)
  53.  
  54. If Left(bonusText, 1) = "$" Then
  55. wsTarget.Cells(i, 13).Value = CDbl(Mid(bonusText, 2)) ' USD column
  56. wsTarget.Cells(i, 12).Value = "" ' Clear GBP column
  57. ElseIf Left(bonusText, 1) = "£" Then
  58. wsTarget.Cells(i, 12).Value = CDbl(Mid(bonusText, 2)) ' GBP column
  59. wsTarget.Cells(i, 13).Value = "" ' Clear USD column
  60. End If
  61.  
  62. ' Extract Year and Month
  63. If Not IsEmpty(wsTarget.Cells(i, 4)) Then
  64. wsTarget.Cells(i, 14).Value = Year(wsTarget.Cells(i, 4))
  65. wsTarget.Cells(i, 15).Value = Month(wsTarget.Cells(i, 4))
  66. End If
  67. Next i
  68.  
  69. ' Format columns
  70. With wsTarget
  71. .Range("I:I").NumberFormat = "[h]:mm:ss"
  72. .Range("J:J").NumberFormat = "[$£-en-GB]#,##0.00;-[$£-en-GB]#,##0.00;;@" ' GBP Reward
  73. .Range("K:K").NumberFormat = "[$$-en-US]#,##0.00;-[$$-en-US]#,##0.00;;@" ' USD Reward
  74. .Range("L:L").NumberFormat = "[$£-en-GB]#,##0.00;-[$£-en-GB]#,##0.00;;@" ' GBP Bonus
  75. .Range("M:M").NumberFormat = "[$$-en-US]#,##0.00;-[$$-en-US]#,##0.00;;@" ' USD Bonus
  76. .Range("N:O").NumberFormat = "0" ' Year and Month
  77. End With
  78.  
  79. ' Add conditional formatting for Status column
  80. With wsTarget.Range("G2:G" & lastRow)
  81. .FormatConditions.Delete
  82. With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""APPROVED""")
  83. .Interior.Color = RGB(198, 239, 206) ' Light green
  84. End With
  85. With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""RETURNED""")
  86. .Interior.Color = RGB(173, 216, 230) ' Light blue
  87. End With
  88. With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""AWAITING REVIEW""")
  89. .Interior.Color = RGB(255, 165, 0) ' Light orange
  90. End With
  91. With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""REJECTED""")
  92. .Interior.Color = RGB(216, 54, 0) ' Light red
  93. End With
  94. End With
  95.  
  96. ' Reorganize columns as per Macro3
  97. With wsTarget
  98. .Columns("B:C").Delete Shift:=xlToLeft
  99. .Columns("H:H").Cut
  100. .Columns("B:B").Insert Shift:=xlToRight
  101. .Columns("J:J").Cut
  102. .Columns("C:C").Insert Shift:=xlToRight
  103. .Columns("J:J").Cut
  104. .Columns("D:D").Insert Shift:=xlToRight
  105. .Columns("K:K").Cut
  106. .Columns("E:E").Insert Shift:=xlToRight
  107. .Columns("K:K").Cut
  108. .Columns("H:H").Insert Shift:=xlToRight
  109. .Columns("B:E").ColumnWidth = 12
  110. End With
  111.  
  112. ' Set other column widths
  113. wsTarget.Columns("A").AutoFit ' Study title
  114. wsTarget.Columns("F:M").AutoFit ' All other columns
  115.  
  116. ' Delete Sheet1 if it exists
  117. On Error Resume Next
  118. Application.DisplayAlerts = False
  119. ThisWorkbook.Sheets("Sheet1").Delete
  120. Application.DisplayAlerts = True
  121. On Error GoTo 0
  122.  
  123. Application.ScreenUpdating = True
  124. End Sub
Advertisement
Add Comment
Please, Sign In to add comment