+ Reply to Thread
Results 1 to 4 of 4

Help setting up a Loop

  1. #1
    Registered User
    Join Date
    11-25-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    4

    Help setting up a Loop

    I recently wrote some horrible coding to copy and paste data from source files to the correct location in an output file. While the resulting code does work, it is painful to look at and a hog of system resources. I know that my issue could be resolved by the proper use of looping, but I'm not experienced enough to get it right on my own. I'm hoping that someone here can assist.

    Here is a small snippet of the code itself:

    Please Login or Register  to view this content.
    The first IF involving Workbooks("ASTdailyAnalysis.csv").Sheets("ASTdailyAnalysis").Range("B2").Value checks to make sure that this cell is not empty. This is the field that will contain the product name.
    The second one checks the value of that cell and compares it to the product name field in the template report.

    After that copies data from the source sheet Range("E2") and pastes it to the report template field Range("M2")

    As you can see from my snippet of code, I have created the loop by just throwing repeating code in. This is what I need to clean up.

    Each of the fields needs to increment for up to 60 possible entries in both sheets. Any help on how to fix my mess of ungainly code would be greatly appreciated.

    Thanks in advance,

    -Belz

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    You don't need the checks for Empty.

    If you remove then it'll tidy the code a bit.

    Another thing you can do is create references to the worksheets/books you are working with.

    Please Login or Register  to view this content.
    You can now use these references in the code.

    I know this doesn't help with a loop but once the code is tied up it'll be easier to see what's happening.

    As it is I'm not even sure how many files are involved.

    At first I thought it was 2 but now I'm leaning towards 3.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Help setting up a Loop

    Try something like this...

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    11-25-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Help setting up a Loop

    Norie, I was just starting to re-code with your suggestion when AlphaFrog's post hit.
    AlphaFrog, I can't thank you enough. I tested your code and it works beautifully. You resolved in 36 lines of code an issue that had taken me over 40k lines.

    thank you again,

    -Belz

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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