+ Reply to Thread
Results 1 to 11 of 11

Loop to last populated cell in row

  1. #1
    Forum Contributor
    Join Date
    09-17-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    117

    Loop to last populated cell in row

    I'm looking for a smarter way to run a macro that copies elements of a filtered list to a table.

    The loop currently runs to the defined end column (70) however the actual last populated cell could be in column 30, but it continues to loop though the empty cells.

    I'd like it to stop looping through once it finds the last value in that particular row. I did try End(xlToLeftRight) but not sure of the syntax.

    The relevant code is:
    Please Login or Register  to view this content.
    The line I'm interested in is
    Please Login or Register  to view this content.
    Can someone point me in the right direction please?

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Loop to last populated cell in row

    Please Login or Register  to view this content.
    If someone has helped you then please add to their Reputation

  3. #3
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop to last populated cell in row

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    09-17-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    117

    Re: Loop to last populated cell in row

    Please Login or Register  to view this content.
    This didn't copy any data across, however

    Please Login or Register  to view this content.
    does copy the data, however if there is a blank column within the range followed by a populated column, the code terminates at the first blank column.

    I might have left out a subsequently important detail in my original post....

    The column headings (row 1) are dates and are all populated. The rows underneath (row 2 to end) may or may not contain values. So there could be entries within June and then nothing until December, where the next three months have entries.

    Would it be better to forget the skipping of empty cells, and instead to copy the entire filtered range?
    So start at N2 to end of filtered range?

    As the code specifies
    Please Login or Register  to view this content.
    then can we get lastrow and define the range from $N$2 to end to copy?

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Loop to last populated cell in row

    Maybe use another variable?

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-17-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    117

    Re: Loop to last populated cell in row

    I've used the following

    Please Login or Register  to view this content.
    Is
    Please Login or Register  to view this content.
    cleaner/ more efficient code to use?

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop to last populated cell in row

    Please Login or Register  to view this content.
    The code goes down to the last non-empty value in column A, so it does not stop if there is empty between 2 rows.

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Loop to last populated cell in row

    Did you test them both?

  9. #9
    Forum Contributor
    Join Date
    09-17-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    117

    Re: Loop to last populated cell in row

    I ran both sets 5x each and got the following times:

    Please Login or Register  to view this content.
    Gave an average result of 4 minutes and

    Please Login or Register  to view this content.
    gave an average of 30 seconds.

    I also run the following before each:
    Please Login or Register  to view this content.
    Should there really be such a difference?

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loop to last populated cell in row

    What is I?
    Yes, the code will be slow as you are using active cell and active workbook.
    Why do you need to loop? Please attach your sample to see what is going?

  11. #11
    Forum Contributor
    Join Date
    09-17-2014
    Location
    UK
    MS-Off Ver
    2007
    Posts
    117

    Re: Loop to last populated cell in row

    Attached is the full workbook - the names have been changed to protect the innocent!

    Sheet 1, DATA is the whole of the source data
    Project Select uses the SelectProject.group module
    By Team uses showteam.byteam and showname.name_data modules.
    Attached Files Attached Files

+ 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] Trying to write a Loop to perform calculations within blank rows between populated rows
    By ObliviousAmI in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 01-15-2014, 06:35 PM
  2. Replies: 1
    Last Post: 11-21-2013, 06:07 AM
  3. [SOLVED] Prompt input of future date in cell when another cell has been populated or modified
    By coffeemints in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2013, 06:46 PM
  4. Need to loop through all populated columns
    By Analyst_Jim in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2011, 07:46 PM
  5. Loops to find blanks then loop to find populated
    By Bevy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-01-2006, 03:55 PM

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