+ Reply to Thread
Results 1 to 11 of 11

Loop to last populated cell in row

Hybrid View

  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:
    10:     ActiveCell.Offset(1, 0).Select
            Do Until ActiveCell.EntireRow.Hidden = False
                ActiveCell.Offset(1, 0).Select
            Loop
            Sheets("By Team").ListObjects("byteam").ListRows.Add AlwaysInsert:=True
                                    
            ActiveWorkbook.Sheets("By Team").Cells(i, 1).Value = ActiveCell.Offset(0, -7).Value
            ActiveWorkbook.Sheets("By Team").Cells(i, 2).Value = ActiveCell.Offset(0, -6).Value
            ActiveWorkbook.Sheets("By Team").Cells(i, 4).Value = ActiveCell.Offset(0, -5).Value
            ActiveWorkbook.Sheets("By Team").Cells(i, 3).Value = ActiveCell.Offset(0, 0).Value
            x = 5
    
            For j = 5 To 70 
            ActiveWorkbook.Sheets("By Team").Cells(i, j).Value = ActiveCell.Offset(0, x).Value
            x = x + 1
            Next
            
            i = i + 1
                If ActiveCell.Value <> "" Then GoTo 10
    The line I'm interested in is
    For j = 5 To 70
    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

    For j = 5 to Range("A:A").End(xlUp).Row
    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

    For j = 5 To Range("A" & Rows.Count).End(xlUp).Row

  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

    For j = 5 to Range("A:A").End(xlUp).Row
    This didn't copy any data across, however

    For j = 5 To Range("A" & Rows.Count).End(xlUp).Row
    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
    Do Until ActiveCell.EntireRow.Hidden = False
    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?

    Sub BuZZarD73()
    Dim z As Long
    
    z = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = 5 To z
    ActiveWorkbook.Sheets("By Team").Cells(i, j).Value = ActiveCell.Offset(0, x).Value
    x = x + 1
    Next
    End Sub

  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

        Sheets(1).UsedRange.Offset(1, 13).Select
        Selection.Resize(Selection.Rows.Count - 1, Selection.Columns.Count - 13).Copy
        Sheets("Project Select").Range("$E$9").PasteSpecial xlPasteValues
    Is
    Dim z As Long
    
    z = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = 5 To z
    ActiveWorkbook.Sheets("By Team").Cells(i, j).Value = ActiveCell.Offset(0, x).Value
    x = x + 1
    Next
    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

    For j = 5 To Range("A" & Rows.Count).End(xlUp).Row
    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:

    Dim z As Long
    
    z = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = 5 To z
    ActiveWorkbook.Sheets("By Team").Cells(i, j).Value = ActiveCell.Offset(0, x).Value
    x = x + 1
    Next
    Gave an average result of 4 minutes and

    Sheets(1).UsedRange.Offset(1, 13).Select
    Selection.Resize(Selection.Rows.Count - 1, Selection.Columns.Count - 13).Copy
    Sheets("Project Select").Range("$E$9").PasteSpecial xlPasteValues
    gave an average of 30 seconds.

    I also run the following before each:
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
        
    End With
    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. [SOLVED] 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