Results 1 to 3 of 3

Ignore empty cells when doing horizontal sort with VBA

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Ignore empty cells when doing horizontal sort with VBA

    I am trying to do a horizontal sort for 6 columns of data per row. I then want to concat all 6 columns into one single cell. The VBA code I am using works fine except for a situation in which there is an empty cell between two columns of data.

    As per the example below, code works fine for first two records. I get a run-time error at the third record because the cell in second column is blank. (sample attached with expected results, if needed). Thanks

    Column 1.........Column 2..............Column3..........Expected Result
    2222...............5555.............................................22225555
    5555...............2222.............................................22225555
    5555............................................2222................22225555

    Sub HorizontalRepSort()
    
    With Sheets("FullFile")
        
        Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Range("W2:W" & Lastrow).ClearContents
        
        For i = 2 To Lastrow
        
            Application.StatusBar = "Analyzing " & i - 1 & " of " & Lastrow - 1 & " records for rep IDs 1-6."
            ActiveRepCount = WorksheetFunction.CountA(.Range("Q" & i & ":V" & i))
            
            'Concat reps IDs 1-6 and sort ascending
            For y = 1 To ActiveRepCount
                MinimumRepValue = Application.WorksheetFunction.Small(.Range("Q" & i).Resize(, ActiveRepCount), y)
                .Range("W" & i) = "'" & .Range("W" & i) & MinimumRepValue
            Next
            
        Next
        
    End With
    
    End Sub
    For example:
    Attached Files Attached Files
    Last edited by maacmaac; 02-17-2012 at 10:50 PM.

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