+ Reply to Thread
Results 1 to 16 of 16

Stack overflow? PLEASE HELP

  1. #1
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Stack overflow? PLEASE HELP

    Hey guys, you've been so much help in the past, I figured I'd bring my >seemingly< final question to you.

    So my code works fine, until I add in a few addition if statements, (by a few I mean it could be just one of the if statements or all 20ish that I'm adding in, the problem still persists)

    Please Login or Register  to view this content.
    I can post the full block of code if needed below.

    Any insight would be appreciated. Please ask any questions and I will get back to them within a moment or two. Thanks!

  2. #2
    Forum Contributor
    Join Date
    09-05-2011
    Location
    Essex, England
    MS-Off Ver
    Excel 2003 Excel 2007
    Posts
    383

    Re: Stack overflow? PLEASE HELP

    can you post a sample workbook with this issue please?

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Stack overflow? PLEASE HELP

    You are making changes to cells within a Worksheet Change event handler. I suspect that the code is generating a loop.

    You need to disable event handling before you make changes and re-enable it afterwards:

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Stack overflow? PLEASE HELP

    Rather than adding all of those value changes in the same sets of ranges, make a separate Sub() and place it in a module and call it from your sheet's change event. Just pass the input parameter values. This is called Modular programming. I can show you if needed.

    The other thing that you should do is to turn off screen updating, maybe automatic calculation, but positively turn off events. The other thing that you should do is to replace If()'s with Select Case. My rule-of-thumb on If()'s is no more than 2. Select Case will not always be the solution but should be considered in cases like yours. By doing these changes, it reduces the stack and makes code maintenance much easier.

    The concepts above, less the call to a Sub() is shown below.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    Hey TMS,

    That solves the stack overflow problem, thanks! Although now it seems the code doesn't seem to run continuously? I can only select the value once and it will populate but then it stops working. I could post a sample workbook but I don't know how to upload it?

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Stack overflow? PLEASE HELP

    Oh, and the event will fire for every change you make. So you will test all the conditions every time you change any cell ... and every time you do that you could find all the conditions are true and change all the cells.

    Regards, TMS

  7. #7
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    Could you please explain what you just said like I'm five? I'm still new to this and don't exactly understand what you mean. Thanks!

  8. #8
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    Hey Kenneth,

    Thanks for the well thought out reply, unfortunately it's so hard for me to understand what you're saying, and that code boggles my mind. If I could upload a sample workbook perhaps that would help more... What does turning off screen updating do, or automatic calculation. I know what the select case is, and I should definitely use that now that you mentioned it. Thanks!

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Stack overflow? PLEASE HELP

    Not sure I can do that. You want to make some changes to a range of cells depending on the value that is entered into one cell ... Range("chan1").

    But, as it stands, whenever you make a change to ANY cell, the event will fire and go through the whole event handler code. It is normal practice to check that the Target cell, the cell that has been changed, is one you are interested in. If it's not, you need to exit early on without executing all the condition checks and changes.

    If it's only firing once, I suspect that you have disabled event handling but, for whatever reason, not re-enabled it after the changes have been made.

    Post the code you have now.

    Regards, TMS

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Stack overflow? PLEASE HELP

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Stack overflow? PLEASE HELP

    To keep all that redundant code from ALL triggering every time, use syntax like this:

    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-29-2015 at 10:53 AM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    So apparently my excel file is 1.2mb and I can't attach anything that big. it would be helpful, I deleted everything that isn't necessary for the most part but I'm unable to upload it. Thanks for the detailed response, that makes sense to me. I put enable events = false before and = enable after (the if statement section) but it still only works once... any way to reduce file size or an alternate place to uplaod?

  13. #13
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    I will try that code, thanks for the insight. What does "option explicit" do?

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,466

    Re: Stack overflow? PLEASE HELP

    Zip it up and upload the zip file

  15. #15
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Stack overflow? PLEASE HELP

    If zip file is too big, you can always upload to free file sharing site like dropbox.com.

    Option Explicit is something that I highly recommend. You can set that from your Visual Basic Editor (VBE): Tools > Options > Editor > Check "Require Variable Declaration" > OK. I set all of those and set Tab to 2. The option requires that you Dim (declare) all of your variables. If you don't know what a command word does, place the cursor in the word or next to it and press F1.

    I shortened Jerry's code to show the concepts that I talked about. Place the first Sub into a Module. Notice that I sent the input parameter types as Variant. I called that routine and passed numbers, not strings, but left the Sub input parameter types as variant in case you had some reason to pass a number as a string.

    Obviously, the other Sub is inserted into the sheet object that you want it to trigger from. Right click the sheet's tab, View Code, and paste it.

    Add the other Cases and code as needed. Be sure to combine those Case matches as Jerry did when your Values were the same.
    Please Login or Register  to view this content.
    Last edited by Kenneth Hobson; 07-29-2015 at 11:45 AM.

  16. #16
    Registered User
    Join Date
    06-10-2015
    Location
    USA
    MS-Off Ver
    2013
    Posts
    37

    Re: Stack overflow? PLEASE HELP

    Here is the link to the sample workbook, it's on dropbox. Thanks for the idea Kenneth. your code is getting more and more complicated and I'm understanding less and less of it. Please help me out here, Thanks for all the help both of you!

    https://www.dropbox.com/s/i7nziazyxg...cert.xlsm?dl=0

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Stack overflow
    By aadpors in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-27-2014, 11:05 AM
  2. Stack overflow when using a VBA code compile spreadsheets.
    By Tolo06 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-18-2013, 10:58 AM
  3. Stack overflow problem
    By g1987 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2012, 08:36 PM
  4. Replies: 3
    Last Post: 12-29-2011, 08:07 AM
  5. Keyboard Cell Delete Runs Into Stack OverFlow With Sheet Change
    By Winon in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2011, 06:30 PM
  6. Stack Overflow
    By DonHuff in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-07-2009, 03:30 PM
  7. [SOLVED] Overflow
    By Erik Beck Jensen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2005, 06:15 AM
  8. stack overflow at line: .....
    By brunetto in forum Excel General
    Replies: 1
    Last Post: 11-19-2005, 07:35 PM

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.6.0 RC 1