Advertisement
veronikaaa86

Exam Prep - VBA

Oct 11th, 2022 (edited)
1,985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub Main()
  2.     Dim lastRow, lastColumn As Integer
  3.     lastRow = Cells(Rows.Count, 1).End(xlUp).row
  4.     lastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
  5.  
  6.     Sheets.Add(Before:=Sheets("Sheet1")).name = "Copy"
  7.    
  8.     CopyData (lastColumn)
  9.     CalculateExpenses (lastRow)
  10.     CalculateProfit (lastRow)
  11.    
  12.     Dim colIndex As Variant
  13.     colIndex = GetColumnAddress("Copy", "Contract Price")
  14.     InsertColumn colIndex, "Expected Delivery Date"
  15.     CalculateExpectedDeliveryDate (lastRow)
  16.    
  17.     InsertColumn 1, "Full Name"
  18.     InsertFullName (lastRow)
  19.    
  20.     Dim colIndexDel As Integer
  21.     colIndexDel = GetColumnAddress("Copy", "First Name")
  22.     Columns(colIndexDel).EntireColumn.Delete
  23.     Columns(colIndexDel).EntireColumn.Delete
  24.    
  25.     Formatting
  26.    
  27.     Range("E" & lastRow + 2) = "=sum(E3:E" & lastRow + 1 & ")"
  28.     Range("F" & lastRow + 2) = "=sum(F3:F" & lastRow + 1 & ")"
  29.     Range("G" & lastRow + 2) = "=sum(G3:G" & lastRow + 1 & ")"
  30.    
  31.     Range("A1").Select
  32. End Sub
  33.  
  34.  
  35.  
  36. Sub InsertFullName(lastRowParam As Integer)
  37.     For i = 2 To lastRowParam
  38.         Dim firstName, lastName As String
  39.         firstName = Range("B" & i)
  40.         lastName = Range("C" & i)
  41.         Range("A" & i) = firstName & " " & lastName
  42.     Next
  43. End Sub
  44.  
  45. Sub CalculateExpectedDeliveryDate(lastRowParam As Integer)
  46.     '=D2+7
  47.    For i = 2 To lastRowParam
  48.         Range("E" & i) = "=D" & i & "+7"
  49.     Next
  50.    
  51. End Sub
  52.  
  53. Function GetColumnAddress(sheetName, targetColName As String)
  54.     Set col = Sheets(sheetName).Rows(1).Find(What:=targetColName)
  55.     If col Is Nothing Then
  56.         GetColumnAddress = False
  57.     Else
  58.         GetColumnAddress = col.Column
  59.     End If
  60. End Function
  61.  
  62. Sub InsertColumn(colIndex As Variant, nameColPar As String)
  63.     Sheets("Copy").Select
  64.    
  65.     Columns(colIndex).EntireColumn.Insert
  66.     Cells(1, colIndex).Value = nameColPar
  67. End Sub
  68.  
  69. Sub CalculateProfit(lastRowParam As Integer)
  70.     For i = 2 To lastRowParam
  71.         '=E2-F2
  72.        Range("G" & i) = "=E" & i & "-F" & i
  73.     Next
  74.    
  75. End Sub
  76.  
  77. Sub CalculateExpenses(lastRowParam As Integer)
  78.     '=E2*0.02
  79.    Sheets("Copy").Select
  80.     For i = 2 To lastRowParam
  81.         Dim contractPrice As Double
  82.         contractPrice = Range("E" & i)
  83.        
  84.         If contractPrice < 10000 Then
  85.             Range("F" & i) = "=E" & i & "*3%"
  86.         ElseIf contractPrice <= 30000 Then
  87.             Range("F" & i) = "=E" & i & "*2.8%"
  88.         ElseIf contractPrice <= 100000 Then
  89.             Range("F" & i) = "=E" & i & "*2.5%"
  90.         Else
  91.             Range("F" & i) = "=E" & i & "*2%"
  92.         End If
  93.        
  94.     Next
  95. End Sub
  96.  
  97. Sub CopyData(lastColumnParam As Integer)
  98.     Sheets("Copy").Select
  99.     For i = 1 To lastColumnParam
  100.         Sheets("Sheet1").Select
  101.         Columns(i).Select
  102.         Selection.Copy
  103.         Sheets("Copy").Select
  104.         Columns(i).Select
  105.         ActiveSheet.Paste
  106.     Next
  107. End Sub
  108.  
  109. Sub Formatting()
  110.     Rows("1:1").Select
  111.     Selection.Insert Shift:=xlDown
  112.     Range("A1:G1").Select
  113.     With Selection
  114.         .HorizontalAlignment = xlCenter
  115.         .VerticalAlignment = xlBottom
  116.         .ReadingOrder = xlContext
  117.     End With
  118.     Selection.Merge
  119.     ActiveCell.FormulaR1C1 = "Clients And Contracs"
  120.     Range("A1:G101").Select
  121.     Selection.Borders(xlDiagonalDown).LineStyle = xlNone
  122.     Selection.Borders(xlDiagonalUp).LineStyle = xlNone
  123.     With Selection.Borders(xlEdgeLeft)
  124.         .LineStyle = xlContinuous
  125.         .Weight = xlThin
  126.     End With
  127.     With Selection.Borders(xlEdgeTop)
  128.         .LineStyle = xlContinuous
  129.         .Weight = xlThin
  130.     End With
  131.     With Selection.Borders(xlEdgeBottom)
  132.         .LineStyle = xlContinuous
  133.         .Weight = xlThin
  134.     End With
  135.     With Selection.Borders(xlEdgeRight)
  136.         .LineStyle = xlContinuous
  137.         .Weight = xlThin
  138.     End With
  139.     With Selection.Borders(xlInsideVertical)
  140.         .LineStyle = xlContinuous
  141.         .ColorIndex = 0
  142.         .TintAndShade = 0
  143.         .Weight = xlThin
  144.     End With
  145.     With Selection.Borders(xlInsideHorizontal)
  146.         .LineStyle = xlContinuous
  147.         .Weight = xlThin
  148.     End With
  149.     ActiveWindow.SmallScroll Down:=87
  150.     Range("A101:D101").Select
  151.     With Selection
  152.         .HorizontalAlignment = xlCenter
  153.         .VerticalAlignment = xlBottom
  154.         .ReadingOrder = xlContext
  155.     End With
  156.     Selection.Merge
  157.     With Selection
  158.         .HorizontalAlignment = xlRight
  159.         .VerticalAlignment = xlBottom
  160.         .ReadingOrder = xlContext
  161.         .MergeCells = True
  162.     End With
  163.     ActiveCell.FormulaR1C1 = "TOTAL:"
  164.     Range("E101").Select
  165.     Range("A1:G1").Select
  166.     With Selection.Interior
  167.         .Pattern = xlSolid
  168.         .PatternColorIndex = xlAutomatic
  169.         .ThemeColor = xlThemeColorAccent5
  170.         .TintAndShade = -0.249977111117893
  171.     End With
  172.     With Selection.Font
  173.         .ThemeColor = xlThemeColorDark1
  174.     End With
  175.     Selection.Font.Bold = True
  176.     Selection.Font.Size = 16
  177.     Range("A2:G2").Select
  178.     With Selection.Interior
  179.         .Pattern = xlSolid
  180.         .PatternColorIndex = xlAutomatic
  181.         .ThemeColor = xlThemeColorAccent5
  182.         .TintAndShade = 0.599993896298105
  183.     End With
  184.     With Selection
  185.         .HorizontalAlignment = xlGeneral
  186.         .VerticalAlignment = xlBottom
  187.         .WrapText = True
  188.         .ReadingOrder = xlContext
  189.     End With
  190.     With Selection
  191.         .HorizontalAlignment = xlCenter
  192.         .VerticalAlignment = xlBottom
  193.         .WrapText = True
  194.         .ReadingOrder = xlContext
  195.     End With
  196.     Selection.Font.Bold = True
  197.     With Selection
  198.         .HorizontalAlignment = xlCenter
  199.         .VerticalAlignment = xlCenter
  200.         .WrapText = True
  201.         .ReadingOrder = xlContext
  202.     End With
  203.     ActiveWindow.SmallScroll Down:=88
  204.     Range("A101:G101").Select
  205.     Selection.Font.Bold = True
  206.     Range("A1").Select
  207. End Sub
  208.  
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement