+ Reply to Thread
Results 1 to 11 of 11

[SOLVED] Identical code, different output - reading from two different Excel files

  1. #1
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    [SOLVED] Identical code, different output - reading from two different Excel files

    Dear members,

    Sorry to disturb you again. I have two code snippets that are identical (except for one variable name):

    Snippet 2
    Please Login or Register  to view this content.
    Snippet 1
    Please Login or Register  to view this content.
    Snippet 1 works, and copies the right information. Snippet 2 doesn't work, as column F remains empty after the code has been executed.
    My suspicion at first that the source file (Filename_From, not shown in the text above) might be corrupted. I recreated the file, but that did not help.

    I have attached the two code files in their entirity below. What are your theories?

    EDIT: added workbooks.
    Attached Files Attached Files
    Last edited by nErD; 01-04-2012 at 08:46 AM. Reason: Adding files

  2. #2
    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,776

    Re: Identical code, different output - reading from two different Excel files

    Why would you have two identical pieces of code barring a variable name?

    Why not use one piece of code, the piece that works, and pass the sheet name to it as a variable? One variable that contains the sheet name.

    You're doubling the maintenance effort and, as you are finding out, the time to debug code that works in one place but not in another.

    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


  3. #3
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Identical code, different output - reading from two different Excel files

    TMSucks: indeed, you have a good point. The issue is, the two codes pertain to two user forms. The respective user forms are different, and depend on a user choise executed in a seperate module (this code is part of a much larger module I am working with).

    My working theory is still that one of the files must be corrupted somehow ... one or several cells barring the way. Right now I am confounded. I will let you know if I figure it out! I will leave this thread open in the mean time.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Identical code, different output - reading from two different Excel files

    Why don't you attach the workbook?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Identical code, different output - reading from two different Excel files

    Quote Originally Posted by royUK View Post
    Why don't you attach the workbook?
    Done; edited the first post.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Identical code, different output - reading from two different Excel files

    Probably this suffices, assuming 'TO_Sheet_OFA' refers to a worksheet
    Please Login or Register  to view this content.
    Basically if you use more than 1 workbook you should refer to those workbooks properly, the same applies to worksheets:

    Please Login or Register  to view this content.
    Last edited by snb; 01-04-2012 at 07:40 AM.



  7. #7
    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,776

    Re: Identical code, different output - reading from two different Excel files

    I'm clearly missing something as there's no forms or code in either of the templates that you have uploaded.


    Regards, TMS

  8. #8
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Identical code, different output - reading from two different Excel files

    Sorry if I have confused you TMShucks!

    snb: indeed, I discovered my error and it was due to bad referncing from my part! My lesson of the day: lean how to reference properly.

    Here is the solution:

    Please Login or Register  to view this content.
    Thanks! I hope this can help someone else; I will tag it with the word "referencing".

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Identical code, different output - reading from two different Excel files

    In VBA you'd better not use 'activate' nor 'select'.
    You can refer to workbooks, worksheets directly as I showed in my example. As yuo can see you don't even need objectvariables to accomplish this.

  10. #10
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: [SOLVED] Identical code, different output - reading from two different Excel file

    You are correct snb; as I was not 100% sure of how you code worked, I used the activate choice to get to work. I will try to modify it according to your code when I understand more about how it works (never done a loop like the one you posted).

  11. #11
    Registered User
    Join Date
    01-03-2012
    Location
    Sweden
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: [SOLVED] Identical code, different output - reading from two different Excel file

    snb: I forgot to ask, what is cl in this example?

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