+ Reply to Thread
Results 1 to 8 of 8

Thread: Stock price averages

  1. #1
    Registered User
    Join Date
    10-25-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    25

    Stock price averages

    I have been building this workbook to do stock price averages and some other features, but out of no where none of my macro buttons works and I have no idea why. The 7 day average was suppose to print in column e and when I added it it just made everything stop working the way it was. Any help would be appreciated. The txt. document is the data that I have been using to compute my data.
    Attached Files Attached Files
    Last edited by jaje21; 11-18-2011 at 11:38 PM.

  2. #2
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: Stock price averages

    Hi Jaje,

    I see that this is an assignment for a class, so all I'll say is

    1. You've referenced a variable called AVG7COL (in two separate procedures), yet you've never declared it (or Const, like your other variables)

    2. When writing code, always add Option Explicit at the top. This FORCES you to declare all variables. If you call a macro/module and there are issues, an error will occur and you'll see exactly why in Debug.

    Hope that helps!

  3. #3
    Registered User
    Join Date
    10-25-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Stock price averages

    Thank you Paul. How does debug work? I click it and it highlights some stuff but I don't get how to read it from there?

  4. #4
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: Stock price averages

    When you click Debug, your code window should appear, and the line highlighted in yellow is usually the one throwing an error.

  5. #5
    Registered User
    Join Date
    10-25-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Stock price averages

    Thank you for the explanation, I don't get this at all though. I am getting the same error. I seriously am getting beyond frustrated with this class. It is required to get into the business program, yet it serves very little function for me and I have no interest in learning how to use excel this way. I have to get a B in this class and I am beyond frustrated with it being an online only class so I have no one I can go to, to get help.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    10-25-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Stock price averages

    I know this is an assignment so I am really not wanting someone to just do it for me. I just need help because in this online class I have had to try to learn by looking at the way part of the assignment was done and filling in the blank. I am sorry if I am not suppose to ask for help on homework.

  7. #7
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: Stock price averages

    Ok, so when clicking the 'Compute 7-day' button you get an error. When clicking debug, the lines of code highlighted in yellow are the calculation lines.
    Sub Compute7DayAvg()
        Dim rowNdx As Integer
        
        rowNdx = FIRSTDATAROW + 2
        Do While Not IsEmpty(Cells(rowNdx, DATECOL).Value)
    
        ' The debug highlights the entire section below, up to the division by 7.
        ' It is all, technically, one line of code, so the whole "line" is highlighted.
        ' ---------------------------------------------------------------------------
            Cells(rowNdx, AVG7COL).Value = (Cells(rowNdx - 6, AVG1COL) _
                                            + Cells(rowNdx - 5, AVG1COL) _
                                            + Cells(rowNdx - 4, AVG1COL) _
                                            + Cells(rowNdx - 3, AVG1COL) _
                                           + Cells(rowNdx - 2, AVG1COL).Value _
                                        + Cells(rowNdx - 1, AVG1COL).Value _
                                        + Cells(rowNdx, AVG1COL).Value) / 7
        ' ---------------------------------------------------------------------------
            rowNdx = rowNdx + 1
        Loop
    End Sub
    When in debug mode, you can hover your mouse over any of the variables and a pop-up will appear with that variable's current value. So if you hover over "AVG7COL", you'll see that it's 5. The problem, you'll see by hovering, is that "rowNdx" has a value of 5, too.

    When you plug 5 into the first bit of code after the = sign, you get:
    (Cells(rowNdx - 6, AVG1COL) _
    Which becomes
    (Cells(5 - 6, 1) _
    See the problem? There is no such cell address for "Cells(-1, 1)". This is occurring because at the beginning of this event you have a line:
    rowNdx = FIRSTDATAROW + 2
    It appears you copied that from the code that generates the 3-day averages, but forgot to change it to FIRSTDATAROW + 4. (Your 1-day average used just "rowNdx = FIRSTDATAROW", so I saw the pattern.)

    Change the 2 to a 4, then run your code again. Notice that when you do, cells in column E appear to be formatted as Date. Change the format of those cells to Number (with or without decimals as necessary). Also, I believe you need to run the 1-day averages first, otherwise the results of the 7-day averages will be 0 because your calculation involves those values in column 1 (A).

    Hopefully that helps explain it, and shows you how to debug a little better.

    Good luck in your class!

  8. #8
    Registered User
    Join Date
    10-25-2011
    Location
    Portland, Oregon
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Stock price averages

    Thank you again Paul. That was amazing help I didn't know that about the debug. I promise to do the last step on my own win or lose.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0