Advertisement
Guest User

vba lingo

a guest
Dec 3rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2. Sub Question_A()
  3.     Selection.End(xlDown).Select
  4.     Range("A48").Select
  5. End Sub
  6.  
  7. Sub Question_B()
  8.     Dim newcode As String
  9.    
  10.     newcode = InputBox("Please Enter a New Code")
  11.    
  12.     Range("a1").End(xlDown).Select
  13.     ActiveCell.Offset(1, 0).Value = newcode
  14. End Sub
  15.  
  16. Sub Question_C()
  17.     Dim newcode As String
  18.     Dim expense As Currency
  19.    
  20.     newcode = InputBox("Please Enter a New Code")
  21.     expense = InputBox("Please Enter the Expected Expense for the new item")
  22.    
  23.     Range("a1").End(xlDown).Select
  24.     ActiveCell.Offset(1, 0).Value = newcode
  25.     ActiveCell.Offset(1, 1).Value = expense
  26. End Sub
  27.  
  28. Sub Question_D()
  29.     Dim newcode As String
  30.     Dim predexpense As Currency
  31.     Dim actexpense As Currency
  32.    
  33.     newcode = InputBox("Please Enter a New Code")
  34.     predexpense = InputBox("Plscsease Enter the Expected Expense for the new item")
  35.     actexpense = InputBox("Please Enter the Actual Expense for the new item")
  36.    
  37.     Range("a1").End(xlDown).Select
  38.     ActiveCell.Offset(1, 0).Value = newcode
  39.     ActiveCell.Offset(1, 1).Value = predexpense
  40.     ActiveCell.Offset(1, 2).Value = actexpense
  41. End Sub
  42.  
  43. Sub Question_E()
  44.     With Range("a1")
  45.         Range(.Offset(1, 0), .End(xlDown)).Name = "ItemCodes"
  46.     End With
  47.     With Range("b1")
  48.         Range(.Offset(1, 0), .End(xlDown)).Name = "PredictedExpense"
  49.     End With
  50.     With Range("c1")
  51.         Range(.Offset(1, 0), .End(xlDown)).Name = "ActualExpense"
  52.     End With
  53. End Sub
  54.  
  55. Sub Question_F()
  56.     Dim item As Range
  57.     Dim predict As Currency
  58.     Dim actual As Currency
  59.    
  60.     With Range("a1")
  61.         Range(.Offset(1, 0), .End(xlDown)).Name = "ItemCodes"
  62.     End With
  63.  
  64.     For Each item In Range("Itemcodes")
  65.         predict = item.Offset(0, 1).Value
  66.         actual = item.Offset(0, 2).Value
  67.        
  68.         With item
  69.             If actual > predict - 100 Then
  70.                 Range(.Offset(0, 0), .End(xlToRight)).Interior.Color = vbYellow
  71.             End If
  72.        
  73.             If actual > predict + 100 Then
  74.                 Range(.Offset(0, 0), .End(xlToRight)).Interior.Color = vbRed
  75.             End If
  76.        
  77.             If actual < predict Then
  78.                 Range(.Offset(0, 0), .End(xlToRight)).Interior.Color = vbGreen
  79.             End If
  80.         End With
  81.     Next
  82. End Sub
  83.  
  84. Sub Question_G()
  85.     Dim counter As Integer
  86.     Dim totalrows As Integer
  87.     Dim totalpred As Currency
  88.     Dim totalactual As Currency
  89.  
  90.     With Range("a1")
  91.         totalrows = Range(.Offset(1, 0), .End(xlDown)).Rows.count
  92.     End With
  93.    
  94.     For counter = 1 To totalrows Step 1
  95.         totalpred = totalpred + Range("a1").Offset(counter, 1).Value
  96.         totalactual = totalactual + Range("a1").Offset(counter, 2).Value
  97.     Next
  98.      MsgBox "The total predicted expenses are: £" & totalpred
  99.      MsgBox "The total actual expenses are: £" & totalactual
  100. End Sub
  101.  
  102. Sub Question_H()
  103.     Dim predict As Currency
  104.     Dim actual As Currency
  105.     Dim difference As Currency
  106.     Dim item As Range
  107.     Dim code As String
  108.    
  109.     With Range("E3")
  110.         Range(.Offset(0, 0), .End(xlDown)).ClearContents
  111.     End With
  112.    
  113.     With Range("a1")
  114.         Range(.Offset(1, 0), .End(xlDown)).Name = "ItemCodes"
  115.     End With
  116.    
  117.     For Each item In Range("ItemCodes")
  118.         predict = item.Offset(0, 1).Value
  119.         actual = item.Offset(0, 2).Value
  120.         difference = actual - predict
  121.         code = item.Value
  122.        
  123.         If difference > 500 Then
  124.             Range("E1").End(xlDown).Select
  125.             ActiveCell.Offset(1, 0).Value = item.Value
  126.             ActiveCell.Offset(1, 1).Value = difference
  127.         End If
  128.     Next
  129. End Sub
  130.  
  131. Sub Question_I()
  132.  
  133.     Dim max As Long
  134.     Dim item As Range
  135.     Dim predict As Currency
  136.     Dim actual As Currency
  137.     Dim code As String
  138.    
  139.     With Range("E1")
  140.         Range(.Offset(2, 0), .End(xlDown)).Name = "ItemCodes"
  141.     End With
  142.    
  143.    
  144.     For Each item In Range("Itemcodes")
  145.         If item.Offset(0, 1).Value > max Then
  146.         max = item.Offset(0, 1).Value
  147.         End If
  148.     Next
  149.    
  150.     For Each item In Range("Itemcodes")
  151.         If item.Offset(0, 1) = max Then
  152.            item.Font.Bold = True
  153.            item.Offset(0, 1).Font.Bold = True
  154.         End If
  155.     Next
  156.    
  157.     With Range("a1")
  158.         Range(.Offset(1, 0), .End(xlDown)).Name = "ItemCodes"
  159.     End With
  160.    
  161.     For Each item In Range("Itemcodes")
  162.         If item.Offset(0, 2) - item.Offset(0, 1) = max Then
  163.             actual = item.Offset(0, 2).Value
  164.             predict = item.Offset(0, 1).Value
  165.             code = item.Value
  166.         End If
  167.     Next
  168.     MsgBox "the maximum difference is: " & max & "This is Item: " & code & "it has a actual of: " & actual & " and a predicted of: " & predict
  169. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement