{"id":13340355,"url":"https://github.com/bishopce16/stock-analysis","last_synced_at":"2026-02-17T11:31:13.890Z","repository":{"id":37024221,"uuid":"504287641","full_name":"bishopce16/stock-analysis","owner":"bishopce16","description":"Using VBA to analysis green stock for Steve, to discover which stocks are worth the investment for his parents.","archived":false,"fork":false,"pushed_at":"2022-06-23T22:52:04.000Z","size":2802,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-08T23:22:30.638Z","etag":null,"topics":["vba-excel","visual-studio-code"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bishopce16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-16T19:52:11.000Z","updated_at":"2022-09-09T00:40:13.000Z","dependencies_parsed_at":"2022-08-19T01:41:48.926Z","dependency_job_id":null,"html_url":"https://github.com/bishopce16/stock-analysis","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/bishopce16/stock-analysis","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bishopce16%2Fstock-analysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bishopce16%2Fstock-analysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bishopce16%2Fstock-analysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bishopce16%2Fstock-analysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bishopce16","download_url":"https://codeload.github.com/bishopce16/stock-analysis/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bishopce16%2Fstock-analysis/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29542522,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T08:11:05.436Z","status":"ssl_error","status_checked_at":"2026-02-17T08:09:38.860Z","response_time":100,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["vba-excel","visual-studio-code"],"created_at":"2024-07-29T19:22:32.465Z","updated_at":"2026-02-17T11:31:13.859Z","avatar_url":"https://github.com/bishopce16.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# stock-analysis\n\n## Overview of Project\n### Purpose \nUsing Visual Basic Application or VBA to analysis green stock for Steve, to discover which stocks are worth the investment for his parents. To accomplished this, we had to find the stock’s ticker, total daily volume and annual return value in the year 2017 and 2018. We created a run button to support Steve in future projects, as well as refactoring the original code to improve its productivity. \n\n## Results \n### Refactoring the Green Stock Analysis Code\nRefactoring the code involved first copying the original code and changing the nested loops and creating four different arrays: tickers, tickerVolumes, tickerStartingPrices, and tickerEndingPrices.\n\n#### Refactored Code\n    Sub AllStocksAnalysisRefactored()\n        Dim startTime As Single\n        Dim endTime  As Single\n\n        yearValue = InputBox(\"What year would you like to run the analysis on?\")\n\n        startTime = Timer\n        \n        'Format the output sheet on All Stocks Analysis worksheet\n        Worksheets(\"All Stocks Analysis\").Activate\n        \n        Range(\"A1\").Value = \"All Stocks (\" + yearValue + \")\"\n        \n        'Create a header row\n        Cells(3, 1).Value = \"Ticker\"\n        Cells(3, 2).Value = \"Total Daily Volume\"\n        Cells(3, 3).Value = \"Return\"\n\n        'Initialize array of all tickers\n        Dim tickers(12) As String\n        \n        tickers(0) = \"AY\"\n        tickers(1) = \"CSIQ\"\n        tickers(2) = \"DQ\"\n        tickers(3) = \"ENPH\"\n        tickers(4) = \"FSLR\"\n        tickers(5) = \"HASI\"\n        tickers(6) = \"JKS\"\n        tickers(7) = \"RUN\"\n        tickers(8) = \"SEDG\"\n        tickers(9) = \"SPWR\"\n        tickers(10) = \"TERP\"\n        tickers(11) = \"VSLR\"\n        \n        'Activate data worksheet\n        Sheets(yearValue).Activate\n        \n        'Get the number of rows to loop over\n        RowCount = Cells(Rows.Count, \"A\").End(xlUp).Row\n        \n        '1a) Create a ticker Index\n        \n        tickerIndex = 0\n\n        '1b) Create three output arrays\n        Dim tickerVolumes(12) As Long\n        Dim tickerStartingPrices(12) As Single\n        Dim tickerEndingPrices(12) As Single\n        \n        \n        '2a) Create a for loop to initialize the tickerVolumes to zero.\n        For i = 0 To 11\n            tickerVolumes(i) = 0\n        \n            \n        Next i\n        \n            \n        '2b) Loop over all the rows in the spreadsheet.\n        For i = 2 To RowCount\n        \n            '3a) Increase volume for current ticker\n            tickerVolumes(tickerIndex) = tickerVolumes(tickerIndex) + Cells(i, 8).Value\n            \n    \n            \n            '3b) Check if the current row is the first row with the selected tickerIndex.\n            'If  Then\n            If Cells(i - 1, 1).Value \u003c\u003e tickers(tickerIndex) Then\n                    tickerStartingPrices(tickerIndex) = Cells(i, 6).Value\n                \n            End If\n                \n                \n            'End If\n            \n            '3c) check if the current row is the last row with the selected ticker\n            'If the next row‚Äôs ticker doesn‚Äôt match, increase the tickerIndex.\n            'If  Then\n            If Cells(i + 1, 1).Value \u003c\u003e tickers(tickerIndex) Then\n                tickerEndingPrices(tickerIndex) = Cells(i, 6).Value\n            \n            \n                \n\n                '3d Increase the tickerIndex.\n                \n                tickerIndex = tickerIndex + 1\n                \n            End If\n            'End If\n        \n        Next i\n        \n        '4) Loop through your arrays to output the Ticker, Total Daily Volume, and Return.\n        For i = 0 To 11\n            \n            Worksheets(\"All Stocks Analysis\").Activate\n            tickerIndex = i\n            Cells(4 + i, 1).Value = tickers(tickerIndex)\n            Cells(4 + i, 2).Value = tickerVolumes(tickerIndex)\n            Cells(4 + i, 3).Value = tickerEndingPrices(tickerIndex) / tickerStartingPrices(tickerIndex) - 1\n            \n            \n        Next i\n        \n        'Formatting\n        Worksheets(\"All Stocks Analysis\").Activate\n        Range(\"A3:C3\").Font.FontStyle = \"Bold\"\n        Range(\"A3:C3\").Borders(xlEdgeBottom).LineStyle = xlContinuous\n        Range(\"B4:B15\").NumberFormat = \"#,##0\"\n        Range(\"C4:C15\").NumberFormat = \"0.0%\"\n        Columns(\"B\").AutoFit\n\n        dataRowStart = 4\n        dataRowEnd = 15\n\n        For i = dataRowStart To dataRowEnd\n            \n            If Cells(i, 3) \u003e 0 Then\n                \n                Cells(i, 3).Interior.Color = vbGreen\n                \n            Else\n            \n                Cells(i, 3).Interior.Color = vbRed\n                \n            End If\n            \n        Next i\n    \n        endTime = Timer\n        MsgBox \"This code ran in \" \u0026 (endTime - startTime) \u0026 \" seconds for the year \" \u0026 (yearValue)\n\n    End Sub\n\n\n\n#### Original Code\n    Sub MacroCheck()\n        Dim testMessage As String\n        testMessage = \"Hello World!\"\n        MsgBox (testMessage)\n        \n    \n    End Sub\n\n    Sub DQAnalysis()\n        Worksheets(\"DQ Analysis\").Activate\n\n        Range(\"A1\").Value = \"DAQO (Ticker: DQ)\"\n\n        'Create a header row\n        Cells(3, 1).Value = \"Year\"\n        Cells(3, 2).Value = \"Total Daily Volume\"\n        Cells(3, 3).Value = \"Return\"\n        Worksheets(\"2018\").Activate\n\n        'set initial volume to zero\n        totalVolume = 0\n\n        Dim startingPrice As Double\n        Dim endingPrice As Double\n\n        'Establish the number of rows to loop over\n        rowStart = 2\n        rowEnd = Cells(Rows.Count, \"A\").End(xlUp).Row\n\n        'loop over all the rows\n        For i = rowStart To rowEnd\n\n            If Cells(i, 1).Value = \"DQ\" Then\n\n                'increase totalVolume by the value in the current row\n                totalVolume = totalVolume + Cells(i, 8).Value\n\n            End If\n\n            If Cells(i - 1, 1).Value \u003c\u003e \"DQ\" And Cells(i, 1).Value = \"DQ\" Then\n\n                startingPrice = Cells(i, 6).Value\n\n            End If\n\n            If Cells(i + 1, 1).Value \u003c\u003e \"DQ\" And Cells(i, 1).Value = \"DQ\" Then\n\n                endingPrice = Cells(i, 6).Value\n\n            End If\n\n        Next i\n\n        Worksheets(\"DQ Analysis\").Activate\n        Cells(4, 1).Value = 2018\n        Cells(4, 2).Value = totalVolume\n        Cells(4, 3).Value = (endingPrice / startingPrice) - 1\n\n\n    End Sub\n\n\n    Sub AllStocksAnalysis()\n\n        Dim startTime As Single\n        Dim endTime  As Single\n\n        yearValue = InputBox(\"What year would you like to run the analysis on?\")\n\n        startTime = Timer\n    '1) Format the output sheet on All Stocks Analysis worksheet\n    Worksheets(\"All Stocks Analysis\").Activate\n    Range(\"A1\").Value = \"All Stocks (\" + yearValue + \")\"\n    'Create a header row\n    Cells(3, 1).Value = \"Ticker\"\n    Cells(3, 2).Value = \"Total Daily Volume\"\n    Cells(3, 3).Value = \"Return\"\n\n    '2) Initialize array of all tickers\n    Dim tickers(11) As String\n    tickers(0) = \"AY\"\n    tickers(1) = \"CSIQ\"\n    tickers(2) = \"DQ\"\n    tickers(3) = \"ENPH\"\n    tickers(4) = \"FSLR\"\n    tickers(5) = \"HASI\"\n    tickers(6) = \"JKS\"\n    tickers(7) = \"RUN\"\n    tickers(8) = \"SEDG\"\n    tickers(9) = \"SPWR\"\n    tickers(10) = \"TERP\"\n    tickers(11) = \"VSLR\"\n    '3a) Initialize variables for starting price and ending price\n    Dim startingPrice As Single\n    Dim endingPrice As Single\n    '3b) Activate data worksheet\n    Worksheets(yearValue).Activate\n    '3c) Get the number of rows to loop over\n    RowCount = Cells(Rows.Count, \"A\").End(xlUp).Row\n\n    '4) Loop through tickers\n    For i = 0 To 11\n        ticker = tickers(i)\n        totalVolume = 0\n        '5) loop through rows in the data\n        Worksheets(yearValue).Activate\n        For j = 2 To RowCount\n            '5a) Get total volume for current ticker\n            If Cells(j, 1).Value = ticker Then\n\n                totalVolume = totalVolume + Cells(j, 8).Value\n\n            End If\n            '5b) get starting price for current ticker\n            If Cells(j - 1, 1).Value \u003c\u003e ticker And Cells(j, 1).Value = ticker Then\n\n                startingPrice = Cells(j, 6).Value\n\n            End If\n\n            '5c) get ending price for current ticker\n            If Cells(j + 1, 1).Value \u003c\u003e ticker And Cells(j, 1).Value = ticker Then\n\n                endingPrice = Cells(j, 6).Value\n\n            End If\n        Next j\n        '6) Output data for current ticker\n        Worksheets(\"All Stocks Analysis\").Activate\n        Cells(4 + i, 1).Value = ticker\n        Cells(4 + i, 2).Value = totalVolume\n        Cells(4 + i, 3).Value = endingPrice / startingPrice - 1\n\n    Next i\n\n        endTime = Timer\n        MsgBox \"This code ran in \" \u0026 (endTime - startTime) \u0026 \" seconds for the year \" \u0026 (yearValue)\n\n    End Sub\n\n    Sub formatAllStocksAnalysisTable()\n    \n    'Formatting\n    Worksheets(\"All Stocks Analysis\").Activate\n    Range(\"A3:C3\").Font.Bold = True\n    Range(\"A3:C3\").Borders(xlEdgeBottom).LineStyle = xlContinuous\n    Range(\"B4:B15\").NumberFormat = \"#,##0\"\n    Range(\"C4:C15\").NumberFormat = \"0.00%\"\n    Columns(\"B\").AutoFit\n        \n    If Cells(4, 3) \u003e 0 Then\n        'Color the cell green\n        Cells(4, 3).Interior.Color = vbGreen\n    ElseIf Cells(4, 3) \u003c 0 Then\n\n        'Color the cell red\n        Cells(4, 3).Interior.Color = vbRed\n\n    Else\n        'Clear the cell color\n        Cells(4, 3).Interior.Color = xlNone\n\n    End If\n    \n        dataRowStart = 4\n        dataRowEnd = 15\n        For i = dataRowStart To dataRowEnd\n\n            If Cells(i, 3) \u003e 0 Then\n\n                'Change cell color to green\n                Cells(i, 3).Interior.Color = vbGreen\n\n            ElseIf Cells(i, 3) \u003c 0 Then\n\n                'Change cell color to red\n                Cells(i, 3).Interior.Color = vbRed\n            Else\n\n                'Clear the cell color\n                Cells(i, 3).Interior.Color = xlNone\n            End If\n\n        Next i\n\n\n    End Sub\n    \n    Sub ClearWorksheet()\n\n        Cells.Clear\n\n    End Sub\n    \n    Sub yearValueAnalysis()\n    yearValue = InputBox(\"What year would you like to run the analysis on?\")\n\n    End Sub\n\nBelow are two screenshots that show the original code’s run times.\n\n![green_stock_VBA_2017_popout.png](https://github.com/bishopce16/stock-analysis/blob/main/Resources/green_stock_VBA_2017_popout.png)\n\n![green_stock_VBA_2018_popout.png](https://github.com/bishopce16/stock-analysis/blob/main/Resources/green_stock_VBA_2018_popout.png)\n\nBelow are two screenshots that show the run time for the refactored code.\n\n![VBA Challenge 2017 popout message](https://github.com/bishopce16/stock-analysis/blob/main/Resources/VBA_Challenge_2017.png)\n\n![VBA Challenge 2018 popout message](https://github.com/bishopce16/stock-analysis/blob/main/Resources/VBA_Challenge_2018.png)\n\nThe run times indicate the refactored code runs about 0.2 seconds faster than the original code which is more efficient. \n\n## Summary Analysis\nBesides a faster run time for the macro code, one of the major advantages of refactoring code is that it also helps create a cleaner code. A cleaner code makes the project easier to read and interpret, which can help in debugging, quicker programming, software and design improvements. Another advantage of refactoring VBA script is that you can use as much of the original code as want or need to, however, without a solid grasp of the VBA syntax you could take a working code and potentially make it unusable.\nThe original code should always be saved, as one of the major disadvantages of refactoring code is that the refactored code could become unusable. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbishopce16%2Fstock-analysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbishopce16%2Fstock-analysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbishopce16%2Fstock-analysis/lists"}