+ Reply to Thread
Results 1 to 13 of 13

Code will not work when followed by more code (does work when stepped through)

  1. #1
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Code will not work when followed by more code (does work when stepped through)

    In this code I am downloading and opening a CSV file from google trends (I call that part one). Hereafter the data is split into two columns and a line graph is made (I call that part two). If the code are to work for you, you'll have to follow the instructions given in the start of the code and putting log-in credentials into the code.

    My problem is that the code works just fine when I am stepping through the whole thing using F8. Then the CSV-file/workbook is opened in the line: Invokepattern.Invoke. Part one also works when run seperately.
    It does however NOT work when followed by more code in the form below nor if I split up the two parts and call the two subs seperately.

    The problem/error occurs in the line in the beginning of part two of the code. The error message is: 'Subscript out of range'. The error makes sense since the csv-file/workbook is not open nor active.
    The weird thing here is that when I then press debug and the debugger highlights where the error is found the CSV-file/workbook is opened!

    Furthermore! I have found that if I force the code to debug after part one using the line of code: 'Stop', the CSV-file/workbook is then again opened!

    Below is the code (Change Format - Menu is an add-in of my own. I have tried both with an without reference to it):

    Please Login or Register  to view this content.
    Last edited by Cashgenerator; 11-30-2015 at 11:13 AM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code will not work when followed by more code (does work when stepped through)

    Where do you actually get the error?

    In the posted code where does part 1 end and part 2 begin?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    If have written/highlighted in the code where part one ends and part two begins. I have also written/highlighted where I get the error.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code will not work when followed by more code (does work when stepped through)

    Declare EndRow as Long.

  5. #5
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    The declaration is no problem in this code since there is only some 30 rows in the CSV-file/workbook. When declare as long I still get an error. Now it seems that the error is permanently: 'Subscript out of range'. Again because the CSV-file/workbook never actually opens until i press debug, or I step through the whole thing.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code will not work when followed by more code (does work when stepped through)

    Does this make a difference?
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    No. The problem is not that Endrow can't count the number of rows it is supposed to. The problem is simply that when I run the code the CSV-file/Workbook that is supposed to be opened in part one of the code does not open! This means that the sheet "report" does not exist when part two of the code is run.
    This however is no problem when stepped through using f8.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code will not work when followed by more code (does work when stepped through)

    I'm sorry but I can't test your code as I don't have UIAutomationClient, so all I can go on is the posted code.

    If the problem is that the sheet reports isn't there, perhaps due to a timing issue, perhaps you should add a check for that.

    PS Where do you actually open Report.csv in the code? I can't see any code that does that, but perhaps I missed something.

  9. #9
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    Just before the end of part one in the line: Invokepattern.Invoke. When I step through the file is opened here.

    I have also tried to add a pause the macro. For as much as 20 seconds. But even though I give the file lots of time to open nothing happens until there is an error and I press debug. Then the file is opened and I can step through the rest of the code and it will work just fine.

  10. #10
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    Btw. how can I add a check for when the file is opened? I guess it is so that the macro will only continue upon confirmed that the file has been opened? That sounds smart

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code will not work when followed by more code (does work when stepped through)

    Is the file always called Report.csv?

    If it is, rather than a timed pause, perhaps something like this.
    Please Login or Register  to view this content.
    Again that's untested but in theory what it should do is loop until a workbook with the name 'Report.csv' is opened in Excel.

    I've added DoEvents to allow the system to process other events just as you have in your code when you navigate to the Google login page.

    Actually, have you considered adding this when you are navigating to the 'trends' page?
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    Thanks for the suggestions.

    Unfortunately I won't be able to test the code before thursday. I will let you know about the result.

  13. #13
    Registered User
    Join Date
    05-22-2015
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Office 2010
    Posts
    11

    Re: Code will not work when followed by more code (does work when stepped through)

    I have now tried both your pieces of code.

    The good news is that the first piece of code works. The bad news is that it doesn't help the problem. The code now enters an infinite loop, and rightly so because the workbook report.csv is never opened. Until I debug of course .

    Thank you for your efforts.

+ 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] Need help with VBA code. Tons of code seperated in two, second part of code doesn't work.
    By FragaGeddon in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 11-30-2015, 11:46 AM
  2. VBA Code for populating cells from one Work Book A to another Work Book with condition
    By ray.kanata in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 06-22-2015, 03:46 PM
  3. Replies: 9
    Last Post: 08-30-2013, 07:49 AM
  4. Replies: 1
    Last Post: 08-14-2013, 12:01 PM
  5. [SOLVED] hELP FOR MY PROJECT (IN PROJECT SHEET CODE NOT WORK,, OUTSIDE THAT CODE WORK)
    By santosh226001 in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-06-2013, 06:53 AM
  6. Code to work at the same time as other code???
    By D_N_L in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-15-2012, 12:43 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