+ Reply to Thread
Results 1 to 2 of 2

loop through many (thousands of) rows find last cell with data, write value to set column

Hybrid View

svancleave loop through many (thousands... 11-18-2019, 12:36 AM
nilem Re: loop through many... 11-18-2019, 02:39 AM
  1. #1
    Registered User
    Join Date
    11-10-2019
    Location
    San Diego County
    MS-Off Ver
    Office Professional Plus 2016
    Posts
    1

    loop through many (thousands of) rows find last cell with data, write value to set column

    I am tasked with cleaning up and rebuilding a filing system that has been unmanaged for 20 yrs. First step was to pull fully qualified file names with owner into text file and upload into Excel. I need to be able to sort on owner, so need "Owner" data to be written to a single column. Fully qualified file names are in some cases 20 directories deep! It's a mess. I used back-slash as delimiter. There are a dozen drives, each with hundreds of thousands of files. This is my first post, and attempted to attach small spread sheet.

    P-code might go like this:

    For each populated row in Worksheet
    find first empty cell in first row
    write offset (0,-1) value to cell in same row first empty column
    move to the next row
    and so on

    I have searched and struggled for some time. All code attempts--and there have been many--end up throwing object-related errors of some kind.

    Here is just one example of failed code. I have tried A1 and R1C1. I would also like to steer away from the three nested levels shown here. Error is always thrown on line with row range reference, like "Range(lngRowCounter, lngLastColumn).Value = rngCell.Offset(0, -1).Value". I am using a named range in this case.

    Sub GetFirstEmpty()
        Dim lngRowCounter As Long
        Dim lngLastRow As Long
        Dim lngLastColumn As Long
        Dim rngCell As Range
        Dim obSheet As Worksheet
        Dim rngRow As Range
    
        Set obSheet = ActiveSheet
        lngLastRow = obSheet.Range("WholeFS").Rows.Count
        lngLastColumn = (obSheet.Range("WholeFS").Columns.Count) + 1
        
        For lngRowCounter = 1 To lngLastRow
        Set rngRow = obSheet.Rows(lngRowCounter)
            For Each rngCell In rngRow.Cells
                If rngCell.Value = "" Then
                    Range(lngRowCounter, lngLastColumn).Value = rngCell.Offset(0, -1).Value
                Else
                End If
            Next rngCell
        Next
    I know I'm missing something, and likely not referring to the target cell properly. Any and all help will be much appreciated!
    Attached Files Attached Files
    Last edited by svancleave; 11-18-2019 at 12:38 AM.

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: loop through many (thousands of) rows find last cell with data, write value to set col

    Hi svancleave,
    maybe so
    Sub GetFirstEmpty()
    Dim r As Range
    'For each populated row in Worksheet
    For Each r In ActiveSheet.Columns(1).SpecialCells(2)
        'find the last non-empty cell in the row
        With Cells(r.Row, Columns.Count).End(xlToLeft)
            'write offset (0,-1) value to cell in same row first empty column
            .Offset(, 1).Value = .Value
        End With
        'move to the next row
    Next r
    End Sub

+ 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. Loop through column to find EID then find date and paste corresponding data
    By CieloSalas in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-01-2017, 06:33 PM
  2. Replies: 2
    Last Post: 06-25-2016, 07:14 PM
  3. CConverting a column with thousands of rows to a number
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2014, 01:45 PM
  4. [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
  5. Need to find last cell in a column with data - but not all rows in that colun have data
    By ILoveStMartin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-14-2013, 07:02 AM
  6. write a formula for every cell in a column for the total number of rows of data
    By excelaspire0219 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2009, 02:30 PM
  7. simple loop through data and write result in cell
    By a94andwi in forum Excel General
    Replies: 6
    Last Post: 01-09-2007, 10:11 AM

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