+ Reply to Thread
Results 1 to 8 of 8

Struggling to get the Loop to work

  1. #1
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Struggling to get the Loop to work

    Hello everyone,

    Imagine if I have more than one row with the current date. My macro works for one row but I want it to work for multiple rows, how can I fix it, here is the code.

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Ok, first things first, get out of the habit of selecting and activating. Reference objects directly. Besides issues arising from not selecting the right object, it takes less code and executes quicker.

    Please Login or Register  to view this content.
    This will result in each new line found in the Risk sheet being inserted into row 2 of the ErrorLog and each previously found line being shifted down to make way for it.

    You could also write it so that each new line gets added to the NEXT line of the ErrorLog:

    Please Login or Register  to view this content.
    Hope this helps.
    Last edited by kadeo; 03-18-2015 at 10:51 AM.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Struggling to get the Loop to work

    The technical answer to your question is; after the first iteration of your loop the active worksheet is changed and the code within the loop incorrecly functions on the active workbook/worksheet.

  4. #4
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Quote Originally Posted by stnkynts View Post
    The technical answer to your question is; after the first iteration of your loop the active worksheet is changed and the code within the loop incorrecly functions on the active workbook/worksheet.
    Perfect example of why you should avoid using Select and Activate!

  5. #5
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Struggling to get the Loop to work

    Thank you for your reply, it looks brilliant. I'm getting an error though, here is the code:

    Please Login or Register  to view this content.
    It highlights Filename and says

    Compile error:

    Expected: end of statement

    Any ideas?

  6. #6
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    ok, Replace this line:
    Please Login or Register  to view this content.
    with this line:
    Please Login or Register  to view this content.
    And see if that works.

  7. #7
    Registered User
    Join Date
    01-08-2015
    Location
    Dublin
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: Struggling to get the Loop to work

    This works

    Please Login or Register  to view this content.
    Thank you for alll your help

  8. #8
    Forum Contributor
    Join Date
    05-27-2014
    Location
    Belfast, Northern Ireland
    MS-Off Ver
    2007 & 2010
    Posts
    273

    Re: Struggling to get the Loop to work

    Cool, Glad I could help. Mark your thread as solved in the thread options. Cheers Lewis.

+ 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] Cannot get loop within a loop to work
    By forrestgump1980 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-04-2015, 10:02 AM
  2. struggling to get 2 IF formulas to work in one cell
    By cab1979 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-07-2014, 11:18 AM
  3. New Tabs for each run of a loop does not work!!!
    By danitouffaha in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2012, 04:57 PM
  4. My loop doesn't work why not
    By ljh66 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2007, 01:02 PM
  5. struggling to create a loop - pls help
    By joule in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2005, 01:25 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