37.1k views
1 vote
In VBA Create a script that loops through all the stocks for one year and outputs the following information: The ticker symbol Yearly change from the opening price at the beginning of a given year to the closing price at the end of that year. The percentage change from the opening price at the beginning of a given year to the closing price at the end of that year. The total stock volume of the stock. The result should match the following image: Moderate solution Add functionality to your script to return the stock with the "Greatest % increase", "Greatest % decrease", and "Greatest total volume". The solution should match the following image: Hard solution Make the appropriate adjustments to your VBA script to enable it to run on every worksheet (that is, every year) at once. NOTE

User Kawty
by
8.4k points

1 Answer

4 votes

To analyze stock data using VBA, create a script that initializes variables, loops through worksheet rows, calculates yearly changes, percentage changes, and total volumes for stocks, and determines records like greatest increases and volumes.

Writing a VBA Script to Analyze Stock Data

To create a VBA script to loop through all the stocks for one year and output key information such as the ticker symbol, yearly change, percentage change, and total stock volume, you will need to use a combination of loops, conditionals, and variables to track the data for each stock. Here's a simplified version of how you can structure the code:

Sub AnalyzeStockData()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

' Initialize variables to store stock info at the start of the loop

Dim openingPrice As Double

Dim closingPrice As Double

Dim yearlyChange As Double

Dim percentChange As Double

Dim totalVolume As LongLong

Dim tickerSymbol As String

' Loop through all rows and calculate the required values

For i = 2 To ws.Rows.Count

'... (code to collect and calculate data)

Next i

'... (code to output data)

Next ws

End Sub

To add functionality for identifying the "Greatest % increase" , "Greatest % decrease", and "Greatest total volume", you would keep track of these values within the loop and display them after all stocks have been processed.

Moreover, by wrapping the stock data analysis in a loop that goes through each worksheet, your VBA script can process multiple sheets, which means it would work for each year if every year's data is on a separate sheet within the workbook.

User Paritosh Kulkarni
by
8.7k points