+ Reply to Thread
Results 1 to 6 of 6

Code loop in Excel 2007

  1. #1
    Registered User
    Join Date
    03-15-2010
    Location
    Cambridge, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Code loop in Excel 2007

    Hi,

    I'm trying to simplify some code by running a loop rather than setting the code to run on each of a number of worksheets within a file. The code works for each worksheet individually and is as follows:

    Please Login or Register  to view this content.

    This continues with Sheets("J2").Select, and so on for up to 20 different sheets. This code takes data from the first and second sheets in the file and enters it into specified cells; currently, this method of applying the code to each worksheet works, and gives the correct outputs. I've tried to condense the code by using a loop, shown below:

    Please Login or Register  to view this content.

    But this gives me blank output. I am fairly new to this, although I did write the original code myself.

    Can anyone give me any pointers please?
    Last edited by TPAEm; 03-15-2010 at 08:50 AM.

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: Code loop in Excel 2007

    Hi,
    First pointer: avoid Goto statements, except in On Error Goto lines. It makes your code really hard to follow (especially when we only have part of it) and is almost always unnecessary.
    Second pointer: A sample workbook showing your layout (dummy data preferably) so we can see exactly what is happening, together with a description (in plain English) of what you actually want to happen makes our lives a lot easier!
    Third: proper code indentation makes it a lot easier to read, and a few comments explaining what is going on wouldn't go amiss.

    At a rough guess, I think you want something like this:
    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Registered User
    Join Date
    03-15-2010
    Location
    Cambridge, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Code loop in Excel 2007

    Sorry about that, I'll see if I can trim the document down next time to upload that, it'll make more sense. The spreadsheet itself is complicated, which is why I failed to give you a good idea of what the data does. I'm trying to comment my code more, which would have also been helpful. I'm still getting to grips with the indentation and layout, as this is my first piece of code that hasn't been adapted form someone else's.

    Your suggestion works, but doesn't loop through the worksheets to input the data into each sheet I need. I'll trim and attach a sample file that works with the old code, so you can see what I'm trying to do.

  4. #4
    Registered User
    Join Date
    03-15-2010
    Location
    Cambridge, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Code loop in Excel 2007

    This is an abridged version of the file that works. At the moment it's designed to work for up to 18 additional worksheets.

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,016

    Re: Code loop in Excel 2007

    I suspect that's because I forgot an important bit while tidying up:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-15-2010
    Location
    Cambridge, England
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Code loop in Excel 2007

    Thank you, with one minor tweak (S=m-1), it works like a dream!

+ 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