F8 (Step in to) produces different results than running (F5) VBA excel code

    • #1

    Hello,

    I'm trying to solve a problem with my code. It will be hard to replicate as the code I'm posting is a sub that is called from another routine, but I'm hoping there is enough description here for someone to point me at possible causes. Another post has a similar problem, and by the looks of that, my poor programming skills are to blame, but beyond that, I'm lost. The post is here:

    http://www.excelforum.com/exce…ut-not-at-full-speed.html

    In short the code searches through each cell of a sheet, and if it finds a cell of a certain colour (determined by the users' selection in a different routine), then it will add a named range to that cell. After much head scratching, I've determined that the code stops working at this IF statement (before the named range is added):

                                                                                        If Cell.Interior.ColorIndex = DBcolorIndex Then                                                                              

    And further more, if I step through the code whilst debugging, the line below produces a different result than if I run the code normally; the result for a yellow cell is '6' if I'm debugging, and '-2' if I run normally.

    Thanks for reading so far. I'm In and out of meetings, so I might not be able to respond straight away. The full routine is here:

    • #2

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    How are you stepping through it at all as your sub takes arguments?

    • #3

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    I add a break point at the top of the routine

    • #4

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    OK - so the code seems to work if call it from a different sub, so the problem is probably further back in my code. That might be harder to look through as the original call comes from a form. Any ideas on what sorts of things I should look for?

    • #5

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    You need to check you're using the same arguments when stepping through.

    And do you have more than one workbook in play?

    • #6

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Quote from kev22;700213

    OK - so the code seems to work if call it from a different sub, so the problem is probably further back in my code. That might be harder to look through as the original call comes from a form. Any ideas on what sorts of things I should look for?

    I've modified the code in my first post so that if you take a new sheet in excel, and colour cell A1 yellow, and run the code on that it should run fine (it seems to work, both running and stepping through.

    However, when I run the program from my userform using this code, I get the problem described above, where it will only run if I stop the code in the middle of running and step through it.

    • #7

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Quote from StephenR;700220

    You need to check you're using the same arguments when stepping through.

    And do you have more than one workbook in play?

    I have two workbooks in play - one that the user wants to add named ranges to, and one that holds the user form and calls the routine. I'm using the same arguments (if I've understood you correctly) - Literally, if I interupt the code using a breakpoint or watch then it works, if I leave it to run by itself, then it doesn't.

    • #8

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    You don't have any workbook references so if workbook 1 is active when you step through but workbook 2 is active when running normally you will get different results.

    • #9

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Quote from StephenR;700233

    You don't have any workbook references so if workbook 1 is active when you step through but workbook 2 is active when running normally you will get different results.

    Nice one, that's the bit I was missing. Thanks Stephen!

    For interest, I updated the code as follows:
    Caller:

                                                                                        Call AddFields(ActiveWorkbook, ActiveSheet, stlocation)                                                                              


    Main Program

                                                                                        Sub AddFields(rangeBook As Workbook, rangeSheet As Worksheet, stlocation As String)                                                                                                                    'Debug.Print stlocation                                                                                                                                                    rangeBook.Activate                                                                              
    • #10

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Good news.

    • #11

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Quote from StephenR;700242

    Good news.

    Hi Again. Actually, I spoke to soon. When I run again today, I find that my modification doesn't work. Is there a better way to reference the workbook, or see if there is indeed something else going on here. I'm not convinced now that it is just a simple issue like the sheet being active, as when I step through, I'm only clicking in the VB window, I'm not manually activating windows to 'take a peek' of whats happening. Seems strange to me - stepping through works fine, running straight through doesn't.

    • #12

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    You could reference everything in your calling sub and have only one argument in AddFields. Don't know the detail of your code, but just make sure you always fully reference ranges. If you refer to the workbook and worksheet when you assign to object variables, you don't need to do anything further. If problems persist you'll have to post a sample workbook.

    • #13

    Re: F8 (Step in to) produces different results than running (F5) VBA excel code

    Quote from StephenR;700394

    You could reference everything in your calling sub and have only one argument in AddFields. Don't know the detail of your code, but just make sure you always fully reference ranges. If you refer to the workbook and worksheet when you assign to object variables, you don't need to do anything further. If problems persist you'll have to post a sample workbook.

    OK I'll give it that a try (much tidier than my code too!), though I think there might be more to this than just the code I've posted here. It seems that the program works better on some workbooks that others. My test workbooks happened to be bad cases, though I'm not clear what they have in common, so I'll explore that next. I think we've taken this as far as we can for now. Thanks for your time all the same, I progressed a lot further and faster for your help, and I've discovered a workaround for now too. Cheers.