Commit 5bf5e1ac authored by tinubates@gmail.com's avatar tinubates@gmail.com

hw2 submission

parent 9958454b
File added
Attribute VB_Name = "Module1"
Sub Main()
'loop through each worksheet and call 2 subs
Dim sheet As Worksheet
Application.ScreenUpdating = False
For Each sheet In ThisWorkbook.Sheets
sheet.Select
Call summarize
Call challenge
Next
Application.ScreenUpdating = True
End Sub
Sub clear()
Dim sheet As Worksheet
Application.ScreenUpdating = False
For Each sheet In ThisWorkbook.Sheets
sheet.Select
Range("J:R").clear
Next
Application.ScreenUpdating = True
End Sub
Sub summarize()
'Print summary headers
Range("J1").Value = "Ticker"
Range("K1").Value = "Yearly Change"
Range("L1").Value = "% Change"
Range("M1").Value = "Total Stock Volume"
'Declare variables
Dim lastrow As Long
Dim opeprice As Double
Dim closeprice As Double
Dim yearly As Double
Dim percent As Double
Dim volume As Double
Dim printrow As Integer
'Assign initial values
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
openprice = Cells(2, 3).Value
closeprice = 0
volume = Range("G2").Value
printrow = 2
For i = 2 To lastrow
'if next line is still the same ticker - add to volume
If Cells(i + 1, 1).Value = Cells(i, 1).Value Then
volume = volume + Cells(i + 1, 7).Value
'if next line is a different ticker - assign close price, calculate, print, reset
Else
'assign values and print
closeprice = Cells(i, 6).Value
yearly = closeprice - openprice
'catch divide by 0 error
If openprice <> 0 Then
percent = yearly / openprice
Else
percent = 0
End If
Cells(printrow, 10).Value = Cells(i, 1).Value
Cells(printrow, 11).Value = yearly
Cells(printrow, 12).Value = percent
Cells(printrow, 13).Value = volume
'conditional formatting
If Cells(printrow, 11).Value < 0 Then
Cells(printrow, 11).Interior.ColorIndex = 3
Else
Cells(printrow, 11).Interior.ColorIndex = 4
End If
'reset values
volume = Cells(i + 1, 7).Value
printrow = printrow + 1
openprice = Cells(i + 1, 3).Value
End If
Next i
End Sub
Sub challenge()
'Print challenge headers
Range("Q1").Value = "Ticker"
Range("R1").Value = "Value"
Range("P2").Value = "Greatest % increase"
Range("P3").Value = "Greatest % decrease"
Range("P4").Value = "Greatest total volume"
'Declare variables
Dim lastrow As Long
Dim increase, decrease, volume As Double
Dim increaseticker, decreaseticker, volumeticker As String
'Assign initial values
lastrow = Cells(Rows.Count, 10).End(xlUp).Row
increase = 0
decrease = 0
volume = 0
For i = 2 To lastrow
'check for largest increase
If Cells(i, 12).Value > increase Then
increase = Cells(i, 12).Value
increaseticker = Cells(i, 10).Value
End If
'check for largest decrease
If Cells(i, 12).Value < decrease Then
decrease = Cells(i, 12).Value
decreaseticker = Cells(i, 10).Value
End If
'check for largest volume
If Cells(i, 13).Value > volume Then
volume = Cells(i, 13).Value
volumeticker = Cells(i, 10).Value
End If
Next i
Range("Q2").Value = increaseticker
Range("Q3").Value = decreaseticker
Range("Q4").Value = volumeticker
Range("R2").Value = increase
Range("R3").Value = decrease
Range("R4").Value = volume
End Sub
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment